On 5/26/15 9:47 AM, Sylvain Martel wrote:
Hi,
Not knowing much about SQL stuff, I used Flask-Migrate, which use
alembic, to deal with migrations. But now I have to deal with a
migration where I can't simply destroy the SQLite database and remake
it.(which is how I dealt so far when an ALTER constraint message
popped up)
So I read the doc on batch mode for SQLite, but I admit it makes no
sense to me. How can I take this line in the migration script
op.create_foreign_key(None, 'incomes', 'classtype',
['income_classtype'], ['id'])
and transform it to use batch mode so it works with SQLite?
Well one thing to note is that SQLite doesn't enforce foreign key
constraints anyway unless you are enabling constraints on each
connection. So typical real-world use case, adding an FK constraint is
not very useful unless you want to reflect it later.
Batch mode just means, take any op.XYZ() you want, and throw it
underneath op.batch_alter_table(), and remove the tablename arg, that's it:
def upgrade():
with op.batch_alter_table('incomes') as batch_op:
batch_op.create_foreign_key(None, 'classtype',
['income_classtype'], ['id'])
http://alembic.readthedocs.org/en/latest/ops.html#alembic.operations.BatchOperations.create_foreign_key
Basically, I'm adding a column to 2 tables with a one-to-one
relationship. Here are the models in case it helps. The new columns
are the last line in both models.
class Income(db.Model):
__tablename__='incomes' id = db.Column(db.Integer,primary_key=True)
date = db.Column(db.DateTime)
amount = db.Column(Numeric)
user_ = db.Column(db.Integer, db.ForeignKey('users.id'))
income_classtype = db.Column(db.Integer,db.ForeignKey('classtype.id'))
class ClassType(db.Model):
__tablename__ ='classtype' id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(64),unique=True)
default = db.Column(db.Boolean,default=False,index=True)
class_ = db.relationship('Classes',backref='classtype',lazy='dynamic')
punchcard_type =
db.relationship('Punchcard',backref='classtype',lazy='dynamic')
seasonpass_type =
db.relationship('SeasonPass',backref='classtype',lazy='dynamic')
income_type = db.relationship('Income',backref='classtype',lazy ='dynamic')
Thanks
--
You received this message because you are subscribed to the Google
Groups "sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sqlalchemy-alembic+unsubscr...@googlegroups.com
<mailto:sqlalchemy-alembic+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to sqlalchemy-alembic+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.