question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQLite reflection doesn't detect DEFERRABLE and INITIALLY options on foreign keys

See original GitHub issue

Describe the bug

SQLite supports foreign keys with the DEFERRABLE and INITIALLY options. Reflection doesn’t detect these however, which causes bogus Alembic changes if these are used.

To Reproduce

from sqlalchemy import create_engine, inspect
conn = create_engine("sqlite:///:memory:").connect()
conn.exec_driver_sql("CREATE TABLE test1 (id INTEGER)")
conn.exec_driver_sql("CREATE TABLE test2 (ref INTEGER, CONSTRAINT ref FOREIGN KEY(ref) REFERENCES test1(id) DEFERRABLE INITIALLY DEFERRED)")
print(inspect(conn).get_foreign_keys("test2"))

Error

[{'name': 'ref', 'constrained_columns': ['ref'], 'referred_schema': None, 'referred_table': 'test1', 'referred_columns': ['id'], 'options': {}}]

options is empty.

Versions

  • OS: Linux
  • Python: 3.10
  • SQLAlchemy: 1.4.44
  • Database: SQLite
  • DBAPI (eg: psycopg, cx_oracle, mysqlclient): pysqlite

Additional context

No response

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
zzzeekcommented, Nov 29, 2022

This looks short (that was fast too thanks) I can just cherry-pick it just as easily, thanks

1reaction
mgorvencommented, Nov 29, 2022

we can accept PRs for this. We just merged a similar PR for indexes that use sqlite_where and that was backported to 1.4.x…

#8904 for main. Should I make a separate PR for 1.4?

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQLite Foreign Key Support
This document describes the support for SQL foreign key constraints introduced in SQLite version 3.6.19 (2009-10-14). The first section ...
Read more >
Django 2.0: sqlite IntegrityError: FOREIGN KEY constraint failed
If you have ForeignKey constraints they are now enforced at the database level. So make sure you're not violating a foreign key constraint....
Read more >
Defining Constraints and Indexes
Defining Foreign Keys. A foreign key in SQL is a table-level construct that constrains one or more columns in that table to only...
Read more >
1.4.x Milestone - GitHub
SQLite reflection doesn't detect DEFERRABLE and INITIALLY options on foreign keys next release addition to the milestone which indicates this should be in ......
Read more >
SQLite Foreign Key: Enforce Relationships Between Tables
If the SQLite library is compiled with foreign key constraint support, the application can use the PRAGMA foreign_keys command to enable or disable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found