I have a table in Postgres that only has default column values (id, created_at).
The only way I can insert into this table is
INSERT INTO pages(id) VALUES(DEFAULT) RETURNING id;
Why can't I do this:
INSERT INTO pages RETURNING id;
Just curious.
You can use either :
INSERT INTO test DEFAULT VALUES returning id;
And all the explanations you want are right here : https://www.postgresql.org/docs/current/sql-insert.html
The syntax of PostgreSQL is quite imposed.
DEFAULT VALUES : All columns will be filled with their default values. (An OVERRIDING clause is not permitted in this form.)
you need the values keyword, otherwise how would you tell how many rows you want to insert? However, you do not require the field names, so you can shorten your query a (very) little bit:
INSERT INTO pages VALUES(DEFAULT) RETURNING id;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With