Including database name in schema #1188
-
To allow cross-database querying I want tot add a database name to the schema in table_args. However, this makes alembic see the schema as a new schema when I try to autogenerate a revision. Some example code I tested in a database called example_db:
for the first revision. When I add the database name to the schema like this:
a new revision will detect an added and a removed table, even though they are the same. I'm also unable to upgrade as alembic will tell me there already is an object called example_model in the database. Interestingly enough the other way around works fine: alembic will detect no differences if you first include the database name in the schema and remove it after. Should I even attempt to include database names in the schema or should I look for another solution? If including them is fine, how should I do it? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
the only thing I can see doing here is keeping that schema as "raw" but when you do your queries set up the engine with a schema translate map: https://docs.sqlalchemy.org/en/20/core/connections.html#translation-of-schema-names |
Beta Was this translation helpful? Give feedback.
-
@zzzeek , I was a bit too hasty in concluding everything worked as I hoped. Seems like this works for different schemas, but not for the combination of database + schema. So I'm back to trying to add the database name to the table_args. Is there a way to change my alembic config to make alembic recognise the database name from the table_args attribute as the database name? I presume I need to edit the alembic.ini or overwrite the from alembic.config import Config object somehow, but could not find a matching example in the cookbook or docs. I am using a standard single-database alembic.ini at the moment. Also, let me know if you prefer I make a new discussion for this question as it's slightly different. |
Beta Was this translation helpful? Give feedback.
-
I get what you're saying, but I do think we need to include the DB name for what we're trying to do. To make sure what we're trying to accomplish and the problems are clear:
The alternative would be to always write revisions by hand, but it would be easier if we get alembic to understand the database names as part of the schema. |
Beta Was this translation helpful? Give feedback.
the only thing I can see doing here is keeping that schema as "raw" but when you do your queries set up the engine with a schema translate map: https://docs.sqlalchemy.org/en/20/core/connections.html#translation-of-schema-names