You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am developing a multi-tenant system where each tenant will have it's own schema
I have a "shared" schema for couple of central tables and then one schema per tenant.
In SQLModel, I have set the "shared" schema for my central tables table_args = {"schema": "shared"}
but would like to avoid adding: table_args = {"schema": "tenant"}
to all my tables, because I would then also have to add the schema to all my foreign keys
status_id: int = Field(foreign_key="status.id")
I do not let SQLModel create the tables but use alembic for that : I will have to replace the dummy "tenant" will a real tenant name (and generate the tables for each new tenant), but it would like to avoid also having to update all the foreign keys
I found the following command to set the default schema in SQL Alchemy, but I am not sure where (and if) and I can use that with SQLModel and if it would actually works as expected (so that my foreign keys will also use the default schema if not explicetly specified)
metadata = sa.MetaData(schema="tenant")
Base = declarative_base(metadata=metadata)
I have put in the example code above the code I am using to setup the async engine / session.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
Hello,
I am developing a multi-tenant system where each tenant will have it's own schema
I have a "shared" schema for couple of central tables and then one schema per tenant.
In SQLModel, I have set the "shared" schema for my central tables
table_args = {"schema": "shared"}
but would like to avoid adding:
table_args = {"schema": "tenant"}
to all my tables, because I would then also have to add the schema to all my foreign keys
status_id: int = Field(foreign_key="status.id")
I do not let SQLModel create the tables but use alembic for that : I will have to replace the dummy "tenant" will a real tenant name (and generate the tables for each new tenant), but it would like to avoid also having to update all the foreign keys
I found the following command to set the default schema in SQL Alchemy, but I am not sure where (and if) and I can use that with SQLModel and if it would actually works as expected (so that my foreign keys will also use the default schema if not explicetly specified)
metadata = sa.MetaData(schema="tenant")
Base = declarative_base(metadata=metadata)
I have put in the example code above the code I am using to setup the async engine / session.
Can this be achieved ?
Operating System
Windows
Operating System Details
Ubuntu 20.04 (WSL2)
SQLModel Version
0.0.8
Python Version
3.10.2
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions