要在db中创建新表,我写这样的东西:
class Cat(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(40))
age = db.Column(db.String(40))
born_date = db.Column(db.DateTime, default=datetime.datetime.utcnow)
is_feline = db.Column(db.Boolean)
class Dog(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(40))
age = db.Column(db.String(40))
born_date = db.Column(db.DateTime, default=datetime.datetime.utcnow)
wave_tail = db.Column(db.Boolean)
仅创建了2个表。下大雨。经典之作,来自教程。
我想要这样做
class Animal(object):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(40))
age = db.Column(db.String(40))
born_date = db.Column(db.DateTime, default=datetime.datetime.utcnow)
common_functions
class Cat extends Animal:
is_feline = db.Column(db.Boolean)
def override common_functions
def specific functions
class Dog extends Animal:
wave_tail = db.Column(db.Boolean)
只创建了2个表。猫与狗。
还是有一个地方我可以学习这个。 SQLAlchemy文档非常庞大......
答案 0 :(得分:2)
同时从SQLAlchemy Model类和父类继承您的实体:
class Animal(object):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(40))
age = db.Column(db.String(40))
born_date = db.Column(db.DateTime, default=datetime.datetime.utcnow)
def hi(self):
print 'hello!'
class Cat(Animal, db.Model):
__tablename__ = 'cats'
is_feline = db.Column(db.Boolean)
def hi(self):
print 'cat'
def hello(self):
print 'hello'
class Dog(Animal, db.Model):
__tablename__ = 'dogs'
wave_tail = db.Column(db.Boolean)