开发者

Managing seed data with SqlAlchemy and SqlAlchemy-migrate

开发者 https://www.devze.com 2023-01-27 03:04 出处:网络
I\'m using SqlAlchemy in my Pylons application to access data and SqlAlchemy-migrate to maintain the database schema.

I'm using SqlAlchemy in my Pylons application to access data and SqlAlchemy-migrate to maintain the database schema.

It works fine for managing the schema itself. However, I also want to mana开发者_如何学编程ge seed data in a migrate-like way. E.g. when ProductCategory table is created it would make sense to seed it with categories data.

Looks like SqlAlchemy-migrate does not support this directly. What would be a good approach to do this with Pylons+SqlAlchemy+SqlAlchemy-migrate?


Well what format is your seed data starting out in? The migrate calls are just python methods so you're free to open some csv, create SA object instances, loop, etc. I usually have my seed data as a series of sql insert statements and just loop over them executing a migate.execute(query) for each one.

So I'll first create the table, loop and run seed data, and then empty/drop table on the downgrade method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号