Python下使用sqlalchemy关联MySQL数据库,建立主外键关联关系,实现相互约束和级联删除
2017-09-06 21:28
1291 查看
主外键是关系型数据库提供的一直关联表的一种方法,这里的级联关系提供了至少两个功能。
第一:Machine表中的id字段为其主键,Car表中的machine_id为外键,这样就对Car中的machine_id字段进行了约束,使得machine_id字段的值必须是Machine表中id中的其中一个;
第二:提供了两个表数据的级联删除功能,relation中的cascade和ondelete等属性值的设定,使得两个表直接在数据删除时有级联删除的功能、
from sqlalchemy import Column, Integer, String,schema
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.sql.schema import ForeignKey
def table_args():
return {
'mysql_engine': 'InnoDB',
'mysql_charset': 'utf8'
}
Base = declarative_base()
class Machine(Base):
"""defined a Machine"""
__tablename__ = 'machine'
__table_args__ = (
schema.UniqueConstraint('id', name = 'uniq_machine_id'),
table_args()
)
id = Column(Integer, primary_key=True,autoincrement='auto',nullable=False)
name = Column(String(255), nullable=False)
kind = Column(String(40), nullable=False)
relationship('Car', cascade='all, delete-orphan', passive_deletes=True)
class Car(Base):
"""defined a Car"""
__tablename__ = 'car'
__table_args__ = (
schema.UniqueConstraint('id', name = 'uniq_car_id'),
table_args()
)
id = Column(Integer, primary_key=True,autoincrement='auto',nullable=False)
uuid = Column(String(36), nullable=False, unique=True)
name = Column(String(255), nullable=False)
number = Column(String(40), nullable=False)
machine_id = Column(String(36),
ForeignKey('machine.id', ondelete='CASCADE'),
nullable=False)
relationship('Machine', backref='car')
第一:Machine表中的id字段为其主键,Car表中的machine_id为外键,这样就对Car中的machine_id字段进行了约束,使得machine_id字段的值必须是Machine表中id中的其中一个;
第二:提供了两个表数据的级联删除功能,relation中的cascade和ondelete等属性值的设定,使得两个表直接在数据删除时有级联删除的功能、
from sqlalchemy import Column, Integer, String,schema
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.sql.schema import ForeignKey
def table_args():
return {
'mysql_engine': 'InnoDB',
'mysql_charset': 'utf8'
}
Base = declarative_base()
class Machine(Base):
"""defined a Machine"""
__tablename__ = 'machine'
__table_args__ = (
schema.UniqueConstraint('id', name = 'uniq_machine_id'),
table_args()
)
id = Column(Integer, primary_key=True,autoincrement='auto',nullable=False)
name = Column(String(255), nullable=False)
kind = Column(String(40), nullable=False)
relationship('Car', cascade='all, delete-orphan', passive_deletes=True)
class Car(Base):
"""defined a Car"""
__tablename__ = 'car'
__table_args__ = (
schema.UniqueConstraint('id', name = 'uniq_car_id'),
table_args()
)
id = Column(Integer, primary_key=True,autoincrement='auto',nullable=False)
uuid = Column(String(36), nullable=False, unique=True)
name = Column(String(255), nullable=False)
number = Column(String(40), nullable=False)
machine_id = Column(String(36),
ForeignKey('machine.id', ondelete='CASCADE'),
nullable=False)
relationship('Machine', backref='car')
相关文章推荐
- 如何在SQL Server 2005中建立主外键关系?如何实现级联删除效果?
- python使用sqlalchemy中间件对MYSQL数据库进行创建、查询、删除操作
- 如何在SQL Server 2005中建立主外键关系?如何实现级联删除效果?
- 如何在SQL Server 2005中建立主外键关系?如何实现级联删除效果?
- python使用rabbitmq实例七,相互关联编号correlation id
- 在Python3下使用tornado和SQLAlchemy实现一个简单的MVC网站
- MYSQL_使用外键约束(constraint)或触发器(trigger)来进行级联更新、删除
- 使用Python实现BT种子和磁力链接的相互转换
- 重新设计NBearV3中的实体关联设计语言,实现数据库完整性约束和级联更新
- Linux中yum手动安装、手动建立仓库文件夹关联实现关联包自动安装、yum相关命令使用
- Python中使用SQLAlchemy连接Mysql数据库2(多表连接操作)
- Python的ORM框架SQLAlchemy使用入门(二)【连接MySql数据库】
- python SQLAlchemy操作mysql数据库:插入,查询,删除,更新
- Python实现方便使用的级联进度信息实例
- Python中使用SQLAlchemy连接Mysql数据库3(事务,编码)
- 【python学习笔记】python使用sqlalchemy连接mysql数据库
- Linux中yum手动安装、手动建立仓库文件夹关联实现关联包自动安装、yum相关命令使用
- python使用rabbitmq实例七,相互关联编号correlation id
- python使用rabbitmq实例七,相互关联编号correlation id(7)
- ylb:使用sql语句实现添加、删除约束