Make primary and foreign_key information available in model_fields() #952
Unanswered
1cadumagalhaes
asked this question in
Questions
Replies: 1 comment
-
I ran into the same sort of problem a while back. I had resorted to using the from sqlalchemy import inspect
mapper = inspect(model) # where model is some SQLModel, with Table=True
for column in mapper.columns:
# Boolean values
primary_key = column.primary_key
nullable = column.nullable
unique = column.unique
index = column.index
# Gather information on the foreign keys
for foreign_key in column.foreign_keys:
foreign_table = foreign_key.column.table.name
foreign_column = foreign_key.column.name If for whatever reason you needed to get the constraints set on the table like from sqlalchemy.sql.schema import UniqueConstraint, CheckConstraint
mapper = inspect(model) # where model is some SQLModel, with Table=True
table = mapper.tables[0]
for constraint in table.constraints:
if isinstance(constraint, UniqueConstraint):
...
if isinstance(constraint, CheckConstaint):
...
|
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.
-
First Check
Commit to Help
Example Code
Description
I'm using astro sdk to make some pipelines, and I use SQLModel in my application.
In astro we describe Tables and sometimes we need to list it's columns, the same way we do in SQLAlchemy.
Since I already have my tables described as SQLModel classes, I wanted to make a function to get these "Column" informations by just passing the class to it.
I managed to get table name, schema, column names and types; but for now, to mark columns as primary or foreign keys I am passing arrays with these informations.
Is there anyway for me to access the SQLAlchemy model generated?
Or maybe any method to access these informations.
I also created an issue on their repo, cause I'll probably keep working on that
astronomer/astro-sdk#2150
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.18
Python Version
3.10.12
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions