python - Flask/SQLAlchemy Relationships -
so reading database relationships , seem puzzled this
below code of miguel grinberg in blog
from app import db class user(db.model): id = db.column(db.integer, primary_key=true) nickname = db.column(db.string(64), index=true, unique=true) email = db.column(db.string(120), index=true, unique=true) posts = db.relationship('post', backref='author', lazy='dynamic') def __repr__(self): return '<user %r>' % (self.nickname) class post(db.model): id = db.column(db.integer, primary_key = true) body = db.column(db.string(140)) timestamp = db.column(db.datetime) user_id = db.column(db.integer, db.foreignkey('user.id')) def __repr__(self): return '<post %r>' % (self.body) correct me if understanding wrong
posts = db.relationship('post', backref='author', lazy='dynamic') the posts attribute having relationship post model posts attribute become? field? can contain? , backref mean? it's being defined author there no author attribute in neither models.
i looked docs , newb have it. still don't quite it.
so tl:dr
what posts become?
what backref do?
posts list can use abstraction in sqlalchemy access. sqlalchemy translate appropriate sql query when use (it indicates has-many relationship)
backref - hints sqlalchemy property in post indicate belongs specific user. in case mistake (look @ comments @ bottom of blog) , should user
Comments
Post a Comment