Replies: 3 comments
-
Maybe something like this would work ...
|
Beta Was this translation helpful? Give feedback.
0 replies
-
I've encountered the same issue and after reading this SQLAlchemy guide I've managed to work around it. Based to the guide your code should look like this: class UpsertByModelMixin(SQLModel):
created_by_id : Optional[int] = Field(default=None, foreign_key="users.id")
created_by: Optional["User"] = Relationship(sa_relationship_kwargs={ 'foreign_keys': '[UpsertByModelMixin.created_by_id]' })
updated_by_id : Optional[int] = Field(default=None, foreign_key="users.id")
updated_by: Optional["User"] = Relationship(sa_relationship_kwargs={ 'foreign_keys': '[UpsertByModelMixin.updated_by_id]' }) |
Beta Was this translation helpful? Give feedback.
0 replies
-
it would be nice if Ex, instead of from sqlmodel import Field, Relationship, SQLModel
class User(SQLModel, table=True):
...
class Example(SQLModel, table=True):
id: int = Field(primary_key=True)
adminc: str | None = Field(default=None, foreign_key="users.username")
techc: str | None = Field(default=None, foreign_key="users.username")
adminc_alt: str | None = Field(default=None, foreign_key="users.username")
techc_alt: str | None = Field(default=None, foreign_key="users.username")
admin_contact: User | None = Relationship(sa_relationship_kwargs={"foreign_keys": 'Network.adminc'})
tech_contact: User | None = Relationship(sa_relationship_kwargs={"foreign_keys": 'Network.techc'})
admin_contact_alt: User | None = Relationship(sa_relationship_kwargs={"foreign_keys": 'Network.adminc_alt'})
tech_contact_alt: User | None = Relationship(sa_relationship_kwargs={"foreign_keys": 'Network.techc_alt'}) It would be nice to be able to write from sqlmodel import Field, Relationship, SQLModel
class User(SQLModel, table=True):
...
class Example(SQLModel, table=True):
id: int = Field(primary_key=True)
adminc: str | None = Field(default=None, foreign_key="users.username")
techc: str | None = Field(default=None, foreign_key="users.username")
adminc_alt: str | None = Field(default=None, foreign_key="users.username")
techc_alt: str | None = Field(default=None, foreign_key="users.username")
admin_contact: User | None = Relationship(local_key='adminc')
tech_contact: User | None = Relationship(local_key='techc')
admin_contact_alt: User | None = Relationship(local_key='adminc_alt')
tech_contact_alt: User | None = Relationship(local_key='techc_alt') |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Use models such as above will result in the following error because the
foreign_keys
argument expects aColumn
(not a SQLModelField
).As such, its impossible right now for anyone to deploy classes with multiple FKs to the same identity and/or use custom named foreign key columns.
Wanted Solution
To be able to specify foreign keys to the same related entity and/or name my fk columns however I choose.
Wanted Code
Alternatives
No response
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.9.5
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions