烧瓶SQLAlchemy的。创建几个相同的所有领域的表 [英] Flask-SQLAlchemy. Create several tables with all fields identical
问题描述
只要在混合类:
$ p $ from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
$ b $ class MyMixin(object):
id = Column(Integer,primary_key = True)
data = Column(String)
$ b class MyModel1(MyMixin,Base):
__tablename__ ='models1'
$ b $ class MyModel2(MyMixin,Base) :
__tablename__ ='models2'
I'm using Flask with its SQLAlchemy extension. I need to define several model classes, which will create tables in MySQL database. The tables will only differ by name, all the field names/datatypes in them will be identical. How do I define the classes for all those tables? I'm thinking of some inheritance, but I'm not quite sure how exactly would I do that.
Just define all your columns in a mix-in class:
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class MyMixin(object):
id = Column(Integer, primary_key=True)
data = Column(String)
class MyModel1(MyMixin, Base):
__tablename__ = 'models1'
class MyModel2(MyMixin, Base):
__tablename__ = 'models2'
这篇关于烧瓶SQLAlchemy的。创建几个相同的所有领域的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!