-
-
Notifications
You must be signed in to change notification settings - Fork 685
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
🐛 Fix allowing using a ForeignKey
directly, remove repeated column construction from SQLModelMetaclass.__init__
and upgrade minimum SQLAlchemy to >=1.4.36
#443
Conversation
have `get_column_from_field` be called once for each field (in `SQLModelMetaclass.__new__`)
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #443 +/- ##
==========================================
- Coverage 98.49% 97.76% -0.74%
==========================================
Files 185 187 +2
Lines 5856 6266 +410
==========================================
+ Hits 5768 6126 +358
- Misses 88 140 +52
☔ View full report in Codecov by Sentry. |
📝 Docs preview for commit 5ad7c6a at: https://631b234f16d4cf27863512be--sqlmodel.netlify.app |
📝 Docs preview for commit eee8e80 at: https://639cdfc1a7c2ed00568a9bc6--sqlmodel.netlify.app |
Hey is there a workaround for cascade FK definition now while this fix is being investigated? 😊 UPD, probably something like this: from sqlmodel import Field, SQLModel, create_engine
from sqlalchemy import Column, Integer
from sqlalchemy.sql.schema import ForeignKey
class User(SQLModel, table=True):
id: int = Field(primary_key=True)
class Post(SQLModel, table=True):
id: int = Field(primary_key=True)
user_id: int = Field(
sa_column=Column("user_id", Integer, ForeignKey("user.id", ondelete="CASCADE")),)
) |
SQLModelMetaclass.__init__
ForeignKey
directly, remove repeated column construction from SQLModelMetaclass.__init__
📝 Docs preview for commit 50a3f76 at: https://7b43a033.sqlmodel.pages.dev |
Awesome, thanks a lot for the thorough explanation @daniil-berg! 🚀 I tweaked it and updated the internals a bit. This will be available in the next version, |
ForeignKey
directly, remove repeated column construction from SQLModelMetaclass.__init__
ForeignKey
directly, remove repeated column construction from SQLModelMetaclass.__init__
and upgrade minimum SQLAlchemy to >=1.4.36
# What This updates SQLModel to version 0.0.11, which solves an [issue](fastapi/sqlmodel#443) with foreign key declaration, and another [issue](fastapi/sqlmodel#58) with AsyncSession type annotations. # Why Prior to this update, attempting to delete a document with an associated index record would throw a foreign key violation error. # Test plan Add a document which results in an index record being created. Delete the same document and verify that - The document is deleted without error - The index records associated with the document are also deleted
Summary
There was a bug in the
SQLModelMetaclass
whereby the SQLAlchemyColumn
objects were constructed twice for each field defined on a table model: First in the meta class'__new__
method and then again in its__init__
method.With these changes the
get_column_from_field
function is called only once for each field, namely inSQLModelMetaclass.__new__
.Example
Construct a table model with a foreign key explicitly constructed via the SQLAlchemy
ForeignKey
class:Without the proposed changes, even just running these class definitions causes an error:
This is fixed with this PR.
Add the following to the example:
There is no more error and the SQL statements are as expected:
PS
The codecov bot is weird... I really don't see how coverage could possibly have decreased. The statement below is also inconsistent with the report, if you click on it. Oh well.