SqlAlchemy Migration Declarative

I changed the tutorial on the SqlAlchemy-Migrate tutorial into the declarative syntax of my Pylons Pyramid project. I can successfully upgrade and downgrade, but the Base.metadata.drop_all (migrate_engine) command Base.metadata.drop_all bothers me. Here is my migration file:

 from sqlalchemy import Column from sqlalchemy.types import Integer, String, DateTime from sqlalchemy.sql import func from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import scoped_session, sessionmaker from zope.sqlalchemy import ZopeTransactionExtension DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension())) Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) email = Column(String(75), unique=True) fullname = Column(String(60)) password = Column(String(51)) last_login = Column(DateTime) date_joined = Column(DateTime, default=func.now()) def upgrade(migrate_engine): # Upgrade operations go here. Don't create your own engine; bind migrate_engine # to your metadata Base.metadata.bind = migrate_engine Base.metadata.create_all(migrate_engine) # IS THIS DANGEROUS? def downgrade(migrate_engine): # Operations to reverse the above upgrade go here. Base.metadata.bind = migrate_engine Base.metadata.drop_all(migrate_engine) # IS THIS DANGEROUS? 

[edit] My question was how to create tables individually. I did not know that this is my question until you ask the wrong question to solve the right question.

+6
source share
1 answer

The correct update solution is to get the table and create it separately, for example:

 def upgrade(migrate_engine): # Upgrade operations go here. Don't create your own engine; bind migrate_engine # to your metadata User.__table__.create(migrate_engine) 

and for lowering:

 def downgrade(migrate_engine): # Operations to reverse the above upgrade go here. User.__table__.drop(migrate_engine) 
+6
source

Source: https://habr.com/ru/post/893333/


All Articles