Last active
May 5, 2021 07:31
-
-
Save przbadu/1c4f2b017444f2e2b9f29d503359ca2c to your computer and use it in GitHub Desktop.
ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Login to psql and run the following | |
-- What is the result? | |
SELECT MAX(id) FROM your_table; | |
-- Then run... | |
-- If your_table_id_seq is missing, then first create it | |
CREATE SEQUENCE tablename_colname_seq; | |
-- This should be higher than the last result. | |
SELECT nextval('your_table_id_seq'); | |
-- If it's not higher... run this set the sequence last to your highest id. | |
-- (wise to run a quick pg_dump first...) | |
BEGIN; | |
-- protect against concurrent inserts while you update the counter | |
LOCK TABLE your_table IN EXCLUSIVE MODE; | |
-- Update the sequence | |
SELECT setval('your_table_id_seq', COALESCE((SELECT MAX(id)+1 FROM your_table), 1), false); | |
COMMIT; | |
-- If that didn't fixed rails error then, run below scripts | |
ALTER TABLE your_table ALTER COLUMN id SET DEFAULT nextval('your_table_id_seq'::regclass); | |
ALTER SEQUENCE your_table_id_seq OWNED BY your_table.id; |
CREATE SEQUENCE chart_of_accounts_id_seq;
BEGIN;
LOCK TABLE chart_of_accounts IN EXCLUSIVE MODE;
SELECT setval('chart_of_accounts_id_seq', COALESCE((SELECT MAX(id)+1 FROM chart_of_accounts), 1), false);
COMMIT;
ALTER TABLE chart_of_accounts ALTER COLUMN id SET DEFAULT nextval('chart_of_accounts_id_seq'::regclass);
ALTER SEQUENCE chart_of_accounts_id_seq OWNED BY chart_of_accounts.id;
CREATE SEQUENCE cards_id_seq;
BEGIN;
LOCK TABLE cards IN EXCLUSIVE MODE;
SELECT setval('cards_id_seq', COALESCE((SELECT MAX(id)+1 FROM cards), 1), false);
COMMIT;
ALTER TABLE cards ALTER COLUMN id SET DEFAULT nextval('cards_id_seq'::regclass);
ALTER SEQUENCE cards_id_seq OWNED BY cards.id;
CREATE SEQUENCE npayments_id_seq;
BEGIN;
LOCK TABLE cards IN EXCLUSIVE MODE;
SELECT setval('npayments_id_seq', COALESCE((SELECT MAX(id)+1 FROM cards), 1), false);
COMMIT;
ALTER TABLE cards ALTER COLUMN id SET DEFAULT nextval('npayments_id_seq'::regclass);
ALTER SEQUENCE npayments_id_seq OWNED BY cards.id;
CREATE SEQUENCE invoices_id_seq;
BEGIN;
LOCK TABLE cards IN EXCLUSIVE MODE;
SELECT setval('invoices_id_seq', COALESCE((SELECT MAX(id)+1 FROM cards), 1), false);
COMMIT;
ALTER TABLE cards ALTER COLUMN id SET DEFAULT nextval('invoices_id_seq'::regclass);
ALTER SEQUENCE invoices_id_seq OWNED BY cards.id;
CREATE SEQUENCE npayment_links_id_seq;
BEGIN;
LOCK TABLE cards IN EXCLUSIVE MODE;
SELECT setval('npayment_links_id_seq', COALESCE((SELECT MAX(id)+1 FROM cards), 1), false);
COMMIT;
ALTER TABLE cards ALTER COLUMN id SET DEFAULT nextval('npayment_links_id_seq'::regclass);
ALTER SEQUENCE npayment_links_id_seq OWNED BY cards.id;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: