encode / databases

Async database support for Python. 🗄
https://www.encode.io/databases/
BSD 3-Clause "New" or "Revised" License
3.85k stars 262 forks source link

NotNullViolationError when inserting a row with all default values #529

Open ghost opened 1 year ago

ghost commented 1 year ago

I have an SQLAlchemy table with one primary key column and one column with a default value (sqlalchemy.sql.functions.now()).

Because both columns in the table have a default value (from a sequence or the PostgreSQL now() function), I should be able to insert a row without a values argument, or with an empty mapping, e.g. database.execute(table.insert()) or database.execute(table.insert(), values={}). But inserting a row either way raises a NotNullViolationError:

asyncpg.exceptions.NotNullViolationError: null value in column "id" of relation "post" violates not-null constraint
DETAIL:  Failing row contains (null, null).

It seems that a default value isn't being generated for the ID column, which is violating its not-null constraint.

The exception isn't raised and the row is inserted if I pass a value, e.g. database.execute(table.insert(), values={"posted_at": datetime.datetime.now()}), but that defeats the purpose of defining a default value in the database schema.

mathause commented 1 year ago

Duplicate of #72?