网站重新制作多久google重新收录,目前做啥网站能致富,路由器建wordpress,代运营公司有哪些sqlacehmy one to one ------detial to descript 关于uselist的使用。如果你使用orm直接创建表关系,实际上在数据库中是可以创建成多对多的关系,如果加上uselistFalse 你会发现你的orm只能查询出来一个#xff0c;如果不要这个参数orm查询的就是多个#xff0c;一对多的… sqlacehmy one to one ------detial to descript 关于uselist的使用。如果你使用orm直接创建表关系,实际上在数据库中是可以创建成多对多的关系,如果加上uselistFalse 你会发现你的orm只能查询出来一个如果不要这个参数orm查询的就是多个一对多的关系。数据库级别如果也要限制可以自行建立唯一键进行约束。
总结就是sqlacehmy One to One 是orm级别限制 sqlacehmy 简单创建实例展示: from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String, DateTime Base declarative_base()engine create_engine(mysqlpymysql://root:123456localhost:3306/test?charsetutf8, echoTrue) class Worker(Base): # 表名 __tablename__ worker id Column(Integer, primary_keyTrue) name Column(String(50), uniqueTrue) age Column(Integer) birth Column(DateTime) part_name Column(String(50)) # 创建数据表Base.metadata.create_all(engine)
该方法引入declarative_base模块生成其对象Base,再创建一个类Worker。一般情况下数据表名和类名是一致的。tablename用于定义数据表的名称可以忽略忽略时默认定义类名为数据表名。然后创建字段id、name、age、birth、part_name最后使用Base.metadata.create_all(engine)在数据库中创建对应的数据表 数据表的删除 删除数据表的时候一定要先删除设有外键的数据表也就是先删除part,然后才能删除worker两者之间涉及外键这是在数据库中删除数据表的规则。对于两种不同方式创建的数据表删除语句也不一样。
Base.metadata.drop_all(engine)
part.drop(bindengine)
part.drop(bindengine) Base.metadata.drop_all(engine) sqlachemy orm create table代码 from sqlalchemy import Column, String, create_engine, Integer, Text
from sqlalchemy.orm import sessionmaker,declarative_base
import time# 创建对象的基类:
Base declarative_base()# 定义User对象:
class User(Base):# 表的名字:__tablename__ wokers# 表的结构:id Column(Integer, autoincrementTrue, primary_keyTrue, uniqueTrue, nullableFalse)name Column(String(50), nullableFalse)sex Column(String(4), nullableFalse)nation Column(String(20), nullableFalse)birth Column(String(8), nullableFalse)id_address Column(Text, nullableFalse)id_number Column(String(18), nullableFalse)creater Column(String(32))create_time Column(String(20), nullableFalse)updater Column(String(32))update_time Column(String(20), nullableFalse, defaulttime.strftime(%Y-%m-%d %H:%M:%S, time.localtime()),onupdatetime.strftime(%Y-%m-%d %H:%M:%S, time.localtime()))comment Column(String(200))# 初始化数据库连接:
engine create_engine(postgresql://postgres:namepwd:port/dbname) # 用户名:密码localhost:端口/数据库名Base.metadata.create_all(bindengine)
可级联删除的写法实例
class Parent(Base):__tablename__ parentid Column(Integer, primary_keyTrue)class Child(Base):__tablename__ childid Column(Integer, primary_keyTrue)parentid Column(Integer, ForeignKey(Parent.id, ondeletecascade))parent relationship(Parent, backrefchildren) sqlachemy 比较好用的orm介绍链接https://www.cnblogs.com/DragonFire/p/10166527.html sqlachemy的级联删除 https://www.cnblogs.com/ShanCe/p/15381412.html 除了以上例子还列举一下创建多对多关系实例 class UserModel(BaseModel):__tablename__ system_user__table_args__ ({comment: 用户表})username Column(String(150), nullableFalse, comment用户名)password Column(String(128), nullableFalse, comment密码)name Column(String(40), nullableFalse, comment姓名)mobile Column(String(20), nullableTrue, comment手机号)email Column(String(255), nullableTrue, comment邮箱)gender Column(Integer, default1, nullableFalse, comment性别)avatar Column(String(255), nullableTrue, comment头像)available Column(Boolean, defaultTrue, nullableFalse, comment是否可用)is_superuser Column(Boolean, defaultFalse, nullableFalse, comment是否超管)last_login Column(DateTime, nullableTrue, comment最近登录时间)dept_id Column(BIGINT,ForeignKey(system_dept.id, ondeleteCASCADE, onupdateRESTRICT),nullableTrue, indexTrue, commentDeptID)dept_part relationship(DeptModel,back_populatesuser_part)roles relationship(RoleModel, back_populatesusers, secondaryUserRolesModel.__tablename__, lazyjoined)positions relationship(PositionModel, back_populatesusers_obj, secondaryUserPositionModel.__tablename__, lazyjoined)class PositionModel(BaseModel):__tablename__ system_position_management__table_args__ ({comment: 岗位表})postion_number Column(String(50), nullableFalse, comment岗位编号)postion_name Column(String(50), nullableFalse, comment岗位名称)remark Column(String(100), nullableTrue, default, comment备注)positon_status Column(Integer, nullableFalse, default0, comment岗位状态)create_user Column(Integer, nullableTrue, comment创建人)update_user Column(Integer, nullableTrue, comment修改人)users_obj relationship(UserModel, back_populatespositions, secondaryUserPositionModel.__tablename__, lazyjoined)class UserPositionModel(BaseModel):__tablename__ system_user_position__table_args__ ({comment: 用户岗位关联表})user_id Column(BIGINT,ForeignKey(system_user.id, ondeleteCASCADE, onupdateRESTRICT),primary_keyTrue, comment用户ID)position_id Column(BIGINT,ForeignKey(system_position_management.id, ondeleteCASCADE, onupdateRESTRICT),primary_keyTrue, comment岗位ID)以上实例是多对多关系主要是由PositionModel进行量表之间的多对多关系的关联 多对多关系查询 Sessionsessionmaker(bindengine)
sessionsSession()
Userobjsessions.query(UserModel).filter(UserModel.id 1).first()
# Positionobjsessions.query(PositionModel).filter(PositionModel.id 14).first()
# Userobj.positions.append(Positionobj)
for item in Userobj.positions:print(item.postion_name)
sessions.commit()
sessions.close()
2个对象之间是通过relationship 关联参数进行 append 来创建关系
还可以通过remove来删除之间的关系