代码如下
tags = db.Table('tags',
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
db.Column('page_id', db.Integer, db.ForeignKey('page.id')),
db.Column('name', db.VARCHAR(64), nullable=False)
)
class Page(db.Model):
id = db.Column(db.Integer, primary_key=True)
num = db.Column(db.Integer)
tags = db.relationship('Tag', secondary=tags, backref=db.backref('pages', lazy='dynamic'))
def __init__(self, num):
self.num = num
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.VARCHAR(32))
def __init__(self, name):
self.name = name
if __name__ == '__main__':
db.create_all()
tag = Tag('test')
db.session.add(tag)
db.session.commit()
page = Page(3843)
db.session.add(page)
db.session.commit()
page.tags.append(tag)
db.session.add(page)
db.session.commit()
报错如下: (_mysql_exceptions.OperationalError) (1364, "Field 'name' doesn't have a default value")
问题来了,怎么插入 name 的值了?????
1
tanywei 2016-03-18 23:54:35 +08:00
tag = Tag('test')
page = Page(3843) page.tags.append(tag) db.session.add(page) db.session.commit() |