Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlAlchemy Migrate Declarative

I've modified the tutorial on the SqlAlchemy-Migrate tutorial to declarative syntax for my Pylons Pyramid project. I can successfully upgrade and downgrade, but I'm concerned about the Base.metadata.drop_all(migrate_engine) command below. 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 individually create tables. I didn't know this was my question until asking the wrong question enough, to get to the correct question.

like image 606
Devin Avatar asked Jul 22 '11 06:07

Devin


People also ask

Is SQLAlchemy deprecated?

Deprecated since version 0.7: As of SQLAlchemy 0.7, the new event system described in Events replaces the extension/proxy/listener system, providing a consistent interface to all events without the need for subclassing.

What is declarative base SQLAlchemy?

A base class stores a catlog of classes and mapped tables in the Declarative system. This is called as the declarative base class. There will be usually just one instance of this base in a commonly imported module. The declarative_base() function is used to create base class. This function is defined in sqlalchemy.

What is migration in SQLAlchemy?

Overview. Inspired by Ruby on Rails' migrations, SQLAlchemy Migrate provides a way to deal with database schema changes in SQLAlchemy projects. Migrate was started as part of Google's Summer of Code by Evan Rosson, mentored by Jonathan LaCour.

What is an alembic migration?

Alembic is a lightweight database migration tool for usage with the SQLAlchemy Database Toolkit for Python.


1 Answers

The proper solution on upgrade is to get the table and create it individually, like such:

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 downgrading:

def downgrade(migrate_engine):
    # Operations to reverse the above upgrade go here.
    User.__table__.drop(migrate_engine)
like image 74
Devin Avatar answered Oct 12 '22 18:10

Devin