Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

NotNullViolationError when inserting a row with all default values #529

@ghost

Description

  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions