From 53dfd3a1d2aa73209ebc8dd171e8438b50d34e39 Mon Sep 17 00:00:00 2001 From: xianhu Date: Tue, 25 Oct 2016 15:58:30 +0800 Subject: [PATCH] update sqlalchemy, add backref --- python_sqlalchemy.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/python_sqlalchemy.py b/python_sqlalchemy.py index 1310c28..7e65530 100644 --- a/python_sqlalchemy.py +++ b/python_sqlalchemy.py @@ -55,10 +55,13 @@ class User(BaseModel): age = sqlalchemy.Column("age", sqlalchemy.Integer, default=0) # 添加角色id外键(关联到Role.id属性) - role_id = sqlalchemy.Column("role_id", sqlalchemy.Integer, sqlalchemy.ForeignKey("Roles.id"), default=-1) + role_id = sqlalchemy.Column("role_id", sqlalchemy.Integer, sqlalchemy.ForeignKey("Roles.id")) # 添加关系属性(关联到role_id外键上) - role = sqlalchemy.orm.relationship("Role", foreign_keys="User.role_id", backref="User_role") + role = sqlalchemy.orm.relationship("Role", foreign_keys="User.role_id") + + # 添加关系属性(关联到role_id外键上),如果使用了这种方式,Role模型中的users可以省略 + # role = sqlalchemy.orm.relationship("Role", foreign_keys="User.role_id", backref=sqlalchemy.orm.backref("users")) # 构建数据模型Role @@ -74,7 +77,7 @@ class Role(BaseModel): name = sqlalchemy.Column("name", sqlalchemy.String(50), unique=True) # 添加关系属性(关联到User.role_id属性上) - users = sqlalchemy.orm.relationship("User", foreign_keys='User.role_id', backref="Role_users") + users = sqlalchemy.orm.relationship("User", foreign_keys="User.role_id") # 利用Session对象连接数据库