Skip to content

Test Cases Fail with ForeignKeyViolation Error When Using SQLModel.metadata.create_all but Pass with Current Alembic Migrations #247

@avirajsingh7

Description

@avirajsingh7

Describe the bug
When running test cases that create database tables using SQLModel.metadata.create_all(test_engine), the tests fail with a sqlalchemy.exc.IntegrityError due to a foreign key constraint violation.

Additionally, running alembic revision --autogenerate -m "add fk constraints" (without making changes to the models) generates a migration file that modifies foreign key constraints and other schema details. After applying this migration, the test cases still fail with the same foreign key violation error.

ERROR app/tests/api/routes/test_responses.py::test_responses_endpoint_success - sqlalchemy.exc.IntegrityError: (psycopg.errors.ForeignKeyViolation) update or delete on table "organization" violates foreign key constraint "credential_organization_id_fkey" on table "credential"
DETAIL:  Key (id)=(15) is still referenced from table "credential".
[SQL: DELETE FROM organization]

The autogenerated migration file contains the following:

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('credential', 'credential',
               existing_type=sa.VARCHAR(),
               nullable=True)
    op.drop_constraint('credential_organization_id_fkey', 'credential', type_='foreignkey')
    op.drop_constraint('credential_project_id_fkey', 'credential', type_='foreignkey')
    op.create_foreign_key(None, 'credential', 'organization', ['organization_id'], ['id'])
    op.create_foreign_key(None, 'credential', 'project', ['project_id'], ['id'])
    op.alter_column('openai_assistant', 'instructions',
               existing_type=sa.TEXT(),
               type_=sqlmodel.sql.sqltypes.AutoString(),
               existing_nullable=False)
    op.create_index(op.f('ix_openai_assistant_assistant_id'), 'openai_assistant', ['assistant_id'], unique=True)
    op.drop_constraint('openai_assistant_organization_id_fkey', 'openai_assistant', type_='foreignkey')
    op.drop_constraint('openai_assistant_project_id_fkey', 'openai_assistant', type_='foreignkey')
    op.create_foreign_key(None, 'openai_assistant', 'organization', ['organization_id'], ['id'])
    op.create_foreign_key(None, 'openai_assistant', 'project', ['project_id'], ['id'])
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'openai_assistant', type_='foreignkey')
    op.drop_constraint(None, 'openai_assistant', type_='foreignkey')
    op.create_foreign_key('openai_assistant_project_id_fkey', 'openai_assistant', 'project', ['project_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key('openai_assistant_organization_id_fkey', 'openai_assistant', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    op.drop_index(op.f('ix_openai_assistant_assistant_id'), table_name='openai_assistant')
    op.alter_column('openai_assistant', 'instructions',
               existing_type=sqlmodel.sql.sqltypes.AutoString(),
               type_=sa.TEXT(),
               existing_nullable=False)
    op.drop_constraint(None, 'credential', type_='foreignkey')
    op.drop_constraint(None, 'credential', type_='foreignkey')
    op.create_foreign_key('credential_project_id_fkey', 'credential', 'project', ['project_id'], ['id'], ondelete='SET NULL')
    op.create_foreign_key('credential_organization_id_fkey', 'credential', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    op.alter_column('credential', 'credential',
               existing_type=sa.VARCHAR(),
               nullable=False)
    # ### end Alembic commands ###

There appears to be a difference between the current database configuration and the intended configuration, as the foreign key constraint violation occurs with both SQLModel.metadata.create_all and the autogenerated migration, suggesting the current database setup lacks proper ondelete behavior (e.g., CASCADE or SET NULL) for the credential_organization_id_fkey constraint.
Still more investigation is needed.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions