from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = \
'sqlite:////home/~~~/code/flasky/db/test.sqlite'
db = SQLAlchemy(app)
class Writer(db.Model):
__tablename__ = "writers"
id = db.Column(db.Integer, primary_key = True)
articls = db.relationship("Article",backref = 'writer')
class Article(db.Model):
__tablename__ = "articls"
id = db.Column(db.Integer, primary_key = True)
writer_id = db.Column(db.Integer, db.ForeignKey("writers.id"))
这样的一段代码里面
articls = db.relationship("Article",backref = 'writer')
这一句的db.relationship会做些什么,我想知道背后的原理,详细点的,谢谢!
如你给的例子里, relationship 给 Writer 新增了一个 articles 属性,内容是以 writer_id 为外键关联的
一组 Article,backref 则指定给 Article 类增加了一个 writer 属性,内容是以 writer_id 为外键关联的 Writer。