Answer the question
In order to leave comments, you need to log in
How can I prevent an empty field from being entered into a table?
Hello, I have
CREATE TABLE myTable (id BIGSERIAL NOT NULL PRIMARY KEY, firstName VARCHAR(50), lastName VARCHAR(50));
INSERT INTO myTable (id, firstName, lastName) VALUES (1, 'field', 'anotherField');
SELECT * FROM myTable;
INSERT INTO myTable(lastName) VALUES('upsertedLastNameOnly orger');
(even though id is NOT NULL) INSERT INTO myTable(lastName) VALUES('upsertedLastNameOnly orger') ON CONFLICT (id) DO UPDATE SET lastName = EXCLUDED.lastName;
Answer the question
In order to leave comments, you need to log in
Where did you see an empty id?
BIGSERIAL - Equivalent to a BIGINT field with a DEFAULT value from an automatically generated sequence (do \d+ myTable
it in psql for fun), so if you don't specify a value for id in the INSERT, it is taken from the sequence.
For the same reason
INSERT INTO myTable(lastName) VALUES('upsertedLastNameOnly orger') ON CONFLICT (id)
does not make sense, because there can be no conflict on id here. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question