Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NotNullViolationError when inserting a row with all default values #529

Open
ghost opened this issue Jan 19, 2023 · 1 comment
Open

NotNullViolationError when inserting a row with all default values #529

ghost opened this issue Jan 19, 2023 · 1 comment

Comments

@ghost
Copy link

ghost commented Jan 19, 2023

  • OS: Windows 11
  • Python version: 3.11.0
  • Databases version: 0.7.0
  • Database backend: PostgreSQL
  • Database driver: asyncpg

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
Copy link

Duplicate of #72?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant