2

I have a database table with about 15 columns, and I'm using sqlalchemy to access the database. If I want to create a new row to add to the database table, and I want the values of every column (except the id) to be null, is there an easier and more elegant way to do this rather than doing:

new_object = table(col1 = None, col2 = None, col3 = None....) //all the way until column 15

The error I get when I only give the value of the id and no other parameters is as follows:

"TypeError: __init__() missing 15 required positional arguments:"...

and then it lists the 15 parameters I didn't assign values to.

7
  • Do you have column defaults on any of them (except the id, which I assume to be of serial type. Commented Jul 7, 2014 at 20:06
  • I don't, I also didn't create the actual database tables so I unfortunately can't edit them Commented Jul 7, 2014 at 20:39
  • You don't need to edit them. No default plays into your hands. Commented Jul 7, 2014 at 20:42
  • So how can I do this using Sqlalchemy? Because each of my mapped classes have an 'init', doing something like: 'new_object = table(id = 4)' will result in an error because I haven't assigned values to the other parameters Commented Jul 7, 2014 at 20:48
  • The verbatim error message of said error should be in your question. That might help to clarify. With a serial column you would not provide a value for the id column ... Commented Jul 8, 2014 at 13:40

1 Answer 1

2

The INSERT statement fills all columns of a table that are no mentioned explicitely with their respective column default. If none has been defined, NULL is the default default (sic!).

Plus, you can instruct Postgres to insert the column default with the key word DEFAULT:

INSERT INTO tbl (id) VALUES (DEFAULT) RETURNING id;

Should do what you are after and return the newly created id.
Not sure how to translate this to your brand of ORM.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.