user3078608
user3078608

Reputation: 103

How to easily initialize a new object with all values null using sqlalchemy

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.

Upvotes: 2

Views: 316

Answers (1)

Erwin Brandstetter
Erwin Brandstetter

Reputation: 656471

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.

Upvotes: 2

Related Questions