1

I have a table in postgres that basically just serves to collect multiple object in a bag. Think like a shopping cart. It only has a uuid that is generated automatically.
How can I insert an empty record into such a table?

INSERT INTO cart() values();

Doesn't work

4
  • Each column has to have a value, but NULL is one possible value and can be taken to mean "nothing". Even better would be to have two tables, one for the cart and one for each item in the cart. Then you can create a cart (maybe with a user_id, created timestamp, etc), but not add any rows to the cart_item table. Then you have a cart with no items in it. Commented Jan 2, 2021 at 17:22
  • That doesn't work insert into inventory("id") values(NULL); still gives me an error. I just need it to create an empty shopping cart for now. I can add more attributes later. Yes there will be cart_items but I need a shopping cart they can link to first Commented Jan 2, 2021 at 17:37
  • 1
    'Doesn't work' is not an error message and provides no information. To answer this we need actual table schema and actual complete error message. Commented Jan 2, 2021 at 17:57
  • The reason it doesn't work is that I'm trying to set id to null, which invalidates the constraint. I'm not trying to diagnose the problem, I'm trying to figure out how to make entries into a table that only contains a primary key Commented Jan 2, 2021 at 18:08

1 Answer 1

3

From the fine INSERT manual:

This section covers parameters that may be used when only inserting new rows. [...]

DEFAULT VALUES
All columns will be filled with their default values, as if DEFAULT were explicitly specified for each column. (An OVERRIDING clause is not permitted in this form.)
[...]
DEFAULT
The corresponding column will be filled with its default value. An identity column will be filled with a new value generated by the associated sequence. For a generated column, specifying this is permitted but merely specifies the normal behavior of computing the column from its generation expression.

So you can use DEFAULT as a value to explicitly use the column's default value:

INSERT INTO cart(your_uuid_column) values(default);

or you can say:

INSERT INTO cart default values;

to use defaults for all the columns.

Sign up to request clarification or add additional context in comments.

1 Comment

alternatively: insert into cart default values;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.