Skip to content

Commit

Permalink
🐛 Fix allowing using a ForeignKey directly, remove repeated column …
Browse files Browse the repository at this point in the history
…construction from `SQLModelMetaclass.__init__` and upgrade minimum SQLAlchemy to `>=1.4.36` (#443)

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
  • Loading branch information
daniil-berg and tiangolo authored Oct 23, 2023
1 parent c213f5d commit 9809b5b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ classifiers = [

[tool.poetry.dependencies]
python = "^3.7"
SQLAlchemy = ">=1.4.29,<2.0.0"
SQLAlchemy = ">=1.4.36,<2.0.0"
pydantic = "^1.8.2"
sqlalchemy2-stubs = {version = "*", allow-prereleases = true}

Expand Down
10 changes: 4 additions & 6 deletions sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,10 @@ def __init__(
base_is_table = True
break
if getattr(cls.__config__, "table", False) and not base_is_table:
dict_used = dict_.copy()
for field_name, field_value in cls.__fields__.items():
dict_used[field_name] = get_column_from_field(field_value)
for rel_name, rel_info in cls.__sqlmodel_relationships__.items():
if rel_info.sa_relationship:
# There's a SQLAlchemy relationship declared, that takes precedence
# over anything else, use that and continue with the next attribute
dict_used[rel_name] = rel_info.sa_relationship
setattr(cls, rel_name, rel_info.sa_relationship) # Fix #315
continue
ann = cls.__annotations__[rel_name]
Expand Down Expand Up @@ -375,9 +371,11 @@ def __init__(
rel_value: RelationshipProperty = relationship( # type: ignore
relationship_to, *rel_args, **rel_kwargs
)
dict_used[rel_name] = rel_value
setattr(cls, rel_name, rel_value) # Fix #315
DeclarativeMeta.__init__(cls, classname, bases, dict_used, **kw)
# SQLAlchemy no longer uses dict_
# Ref: https://github.com/sqlalchemy/sqlalchemy/commit/428ea01f00a9cc7f85e435018565eb6da7af1b77
# Tag: 1.4.36
DeclarativeMeta.__init__(cls, classname, bases, dict_, **kw)
else:
ModelMetaclass.__init__(cls, classname, bases, dict_, **kw)

Expand Down

0 comments on commit 9809b5b

Please sign in to comment.