how i can avoid the existing database table while generating the initial revision using alembic

1,616 views
Skip to first unread message

Vishal Shimpi

unread,
Jun 8, 2022, 10:15:37 AM6/8/22
to sqlalchemy
I am working on fastapi, in which i have created models and i am inteded to create the table in sql server database, however when i am runing my first migration, alembic detected removal of existing table which are not belongs to my work. Can somebody help how i can create my tables and avoid others tables to be removed and recreated again 

Simon King

unread,
Jun 8, 2022, 12:30:48 PM6/8/22
to sqlal...@googlegroups.com
If I understand correctly, you used Alembic's "autogenerate" feature to create your migration script. This feature compares the table definitions in your application with the table definitions in the database and then generates a script to alter the database to match your application.

You can instruct alembic to ignore certain objects in the database using either the "include_name" or "include_object" hooks:


The first example there ("Omitting Table Names from the Autogenerate Process") is probably what you want.

Hope that helps,

Simon

On Wed, Jun 8, 2022 at 3:15 PM Vishal Shimpi <vishal.s...@gmail.com> wrote:
I am working on fastapi, in which i have created models and i am inteded to create the table in sql server database, however when i am runing my first migration, alembic detected removal of existing table which are not belongs to my work. Can somebody help how i can create my tables and avoid others tables to be removed and recreated again 

--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
http://www.sqlalchemy.org/
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/3a89be66-24a0-4215-8b00-b4c11c52bbf9n%40googlegroups.com.

Vishal Shimpi

unread,
Jun 8, 2022, 12:59:10 PM6/8/22
to sqlal...@googlegroups.com
Thank you Simon for your response. yes i am using autogenerate feature. i tried with include_object and include_name hooks. but it won't work for me. after adding hook also alembic touches to existing tables..

if you send the code snipet for env.py file.. that will really help me..

Thank you.

Simon King

unread,
Jun 8, 2022, 1:32:37 PM6/8/22
to sqlal...@googlegroups.com
Did you regenerate your migration script after adding the hooks?

I would start by putting some print statements in the include_name hook to see how it is being called. You should see it called for every object in the database. You can then decide which names to return True for, and which ones to return False for. The example in the docs is probably a reasonable default:

def include_name(name, type_, parent_names):
    print("Checking %s %s" % (type_, name))
    if type_ == "table":
        result = name in target_metadata.tables
    else:
        result = True
    print("Returning %s" % result)
    return result

Use that hook in your env.py and regenerate your migration script. You should see output for each object in the database.

Simon


Reply all
Reply to author
Forward
0 new messages