2014-10-09 14 views
10

ho fatto una domanda (Alembic - sqlalchemy initial migration) su come individuare tabelle utilizzandoAlambicco - sqlalchemy non rileva le tabelle esistenti

target_metadata = Base.metadata 

per

alembic revision --autogenerate -m "initial migration" 

Dopo ho importato i miei modelli di il file env.py sembrava funzionare bene ma non rileva effettivamente le tabelle esistenti, quindi crea un file di migrazione con tutte le tabelle, ad esempio:

def upgrade(): 
    ### commands auto generated by Alembic - please adjust! ### 
    op.create_table('Brand', 
    sa.Column('id', sa.Integer(), nullable=False), 
    sa.Column('name', sa.String(), nullable=True), 
    sa.Column('slug', sa.String(), nullable=True), 
    sa.Column('date_created', sa.DateTime(), nullable=True), 
    sa.Column('date_updated', sa.DateTime(), nullable=True), 
    sa.PrimaryKeyConstraint('id'), 
    schema='Products' 
    ) 

def downgrade(): 
    ### commands auto generated by Alembic - please adjust! ### 
    op.drop_table('ProductFile', schema='Products') 

ho provato:

alembic stamp head 

ma dopo l'esecuzione di questo e in esecuzione autogenerate comando il sistema genera tutti i modelli ancora una volta.

from project.apps.users.models import * 
from project.apps.orders.models import * 
from project.apps.products.models import * 

from project.base import Base, metadata 

# this is the Alembic Config object, which provides 
# access to the values within the .ini file in use. 
config = context.config 

# Interpret the config file for Python logging. 
# This line sets up loggers basically. 
fileConfig(config.config_file_name) 

# add your model's MetaData object here 
# for 'autogenerate' support 
# from myapp import mymodel 
target_metadata = Base.metadata 

Come evitare questo problema?

Edit:

ENV.py:

https://gist.github.com/pypetey/bb65807ce773d8baeaf1 

ho lasciato cadere il db ed ho fatto funzionare una migrazione

(env) D:\projekty\test>alembic revision --autogenerate 
INFO [alembic.migration] Context impl MSSQLImpl. 
INFO [alembic.migration] Will assume transactional DDL. 
INFO [alembic.autogenerate.compare] Detected added table u'Users.Country' 
INFO [alembic.autogenerate.compare] Detected added table u'Products.Brand' 
INFO [alembic.autogenerate.compare] Detected added table u'Users.User' 
INFO [alembic.autogenerate.compare] Detected added table u'Products.Product' 
INFO [alembic.autogenerate.compare] Detected added table u'Products.ProductFile 
' 
INFO [alembic.autogenerate.compare] Detected added table u'Orders.Order' 
INFO [alembic.autogenerate.compare] Detected added table u'Products.Category' 
INFO [alembic.autogenerate.compare] Detected added table u'Products.Review' 
INFO [alembic.autogenerate.compare] Detected added table u'Users.UserAddress' 
INFO [alembic.autogenerate.compare] Detected added table u'Orders.OrderItem' 
INFO [alembic.autogenerate.compare] Detected added table u'Orders.OrderStatus' 
Generating D:\projekty\test\alembic\versions\1c6337c144a7_.py ... done 

(env) D:\projekty\test>alembic upgrade head 
INFO [alembic.migration] Context impl MSSQLImpl. 
INFO [alembic.migration] Will assume transactional DDL. 
INFO [alembic.migration] Running upgrade None -> 1c6337c144a7, empty message 

(env) D:\projekty\test>alembic revision --autogenerate 
INFO [alembic.migration] Context impl MSSQLImpl. 
INFO [alembic.migration] Will assume transactional DDL. 
INFO [alembic.autogenerate.compare] Detected added table u'Users.Country' 
INFO [alembic.autogenerate.compare] Detected added table u'Products.Brand' 
INFO [alembic.autogenerate.compare] Detected added table u'Users.User' 
INFO [alembic.autogenerate.compare] Detected added table u'Products.Product' 
INFO [alembic.autogenerate.compare] Detected added table u'Products.ProductFile 
' 
INFO [alembic.autogenerate.compare] Detected added table u'Orders.Order' 
INFO [alembic.autogenerate.compare] Detected added table u'Products.Category' 
INFO [alembic.autogenerate.compare] Detected added table u'Products.Review' 
INFO [alembic.autogenerate.compare] Detected added table u'Users.UserAddress' 
INFO [alembic.autogenerate.compare] Detected added table u'Orders.OrderItem' 
INFO [alembic.autogenerate.compare] Detected added table u'Orders.OrderStatus' 
Generating D:\projekty\test\alembic\versions\5abb204549f_.py ... done 
+0

Cosa restituisce "corrente alambicco"? – dgilland

+0

Hai provato a eseguire prima la migrazione su un database vuoto? Dite eseguendo 'alembic revision --autogenerate ...', modificando il file di migrazione, se necessario, eseguendo 'alembic upgrade head', ed eseguendo' alembic revision --autogenerate ... 'di nuovo per confermare che viene generato un file di migrazione vuoto? – dgilland

+0

@dgilland Ho crecreated il db, ho eseguito la migrazione e ho eseguito di nuovo l'autogenerazione. Non ha aiutato. Controlla il mio post aggiornato. – Efrin

risposta

8

Ho avuto questo stesso problema esattamente - non so se ti colpisce ancora. Per me, il problema è stato causato dal fatto che lo schema che stavo usando non era il default - penso che la stessa cosa stia accadendo per te, dal momento che stai usando uno schema "Prodotti". Ho pubblicato un problema a:

https://bitbucket.org/zzzeek/alembic/issue/281/autogenerate-fails-to-detect-existing

E con un po 'di orientamento, sono riuscito a risolvere il problema impostando include_schemas=True in entrambi run_migrations_* funzioni nel modulo alambicco/env.py.

Vedere il docs:

Se Vero, autogenerate esegue la scansione attraverso tutti gli schemi situate nella SQLAlchemy get_schema_names() metodo, e comprendono tutte le differenze in tabelle trovate in tutti quegli schemi. Quando si utilizza questa opzione, è possibile utilizzare anche l'opzione EnvironmentContext.configure.include_object per specificare un callable in grado di filtrare le tabelle/gli schemi da includere .

+0

Questa è una parte, ma correlata: possibilità di controllare quali tabelle DB sono considerate "esistenti": http://alembic.zzzcomputing.com/en/latest/api/runtime .html # alembic.runtime.environment.EnvironmentContext.configure.params.include_object e alcune discussioni precedenti su questo argomento: https://groups.google.com/forum/#!topic/sqlalchemy-alembic/2HJ9J6PiQsk – TaiwanGrapefruitTea

Problemi correlati