Alembic drops and recreate index/Foreign Key everytime I create a new migration file #1160
Replies: 3 comments 3 replies
-
your use of the "public" schema explicitly is the likely source of the issue, as this schema is already in the search path and it confuses SQLAlchemy when it it stated explicitly. see https://docs.sqlalchemy.org/en/14/dialects/postgresql.html#remote-schema-table-introspection-and-postgresql-search-path for background. just remove the use of "public." and leave it off, it's implicit assuming you haven't changed the search path. |
Beta Was this translation helpful? Give feedback.
-
Thank you very much Indeed, it works. There is no foreign Key regeneraton. My application creates schema dynamically. There are tables that need to be in Public schema (ControlOrgan, and users) and others need to be created in every new schema (Roles and RolePermissions). With the new setting you suggested, I struggle to remove ControlOrgan, and users from every new schema. And now it seems like my function def include_object(object, name, type_, reflected, compare_to):
if type_ == "table" and name in ['control_organs', 'users']:
return False
return True
def run_migrations_online() -> None:
...
...
# We get the list of avalaible schema
statement = text("""
SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('information_schema',
'pg_catalog', 'pg_toast') ORDER BY schema_name ASC;
""")
result = DB.execute(statement)
list_of_schemas = [value[0] for value in result ]
# Update every table schema
for schema_name in list_of_schemas:
with connectable.connect() as connection:
connection.execute(text('set search_path to "%s"' % schema_name))
if schema_name == 'public':
context.configure(
connection=connection, target_metadata=target_metadata,
#include_schemas=True
)
else:
context.configure(
connection=connection, target_metadata=target_metadata,
include_schemas=True, include_object=include_object
) Do you have a hint about what should i adjust please ? Thanks again for your help |
Beta Was this translation helpful? Give feedback.
-
Ok i'll try and let you know. I'll come back ASAP
I am not sure I understand this part well. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello Every One.
I am in a need of help about alembic migration file. I have a project setup as followed:
My Model file
So my problem is, every time I generate a new migration, alembic drops and recreates, sometime indexes or Foreign keys. Foreign keys/Indexes are well created in the first time in the database, but Alembic regenerates it in the migration file
This is what I often got
Please find below, description of 2 of our tables
Do you guys know what can I set to prevent it from happen.
Thanks a lot for your help
Beta Was this translation helpful? Give feedback.
All reactions