博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
深入SQLAlchemy之SQLAlchemy ORM重构表
阅读量:7010 次
发布时间:2019-06-28

本文共 4713 字,大约阅读时间需要 15 分钟。

hot3.png

简单介绍:

说明: 此模块主要用于将关系型表映射到PY的类,行映射到PY类的实例,列映射为PY实例的属性,由于其兼容众多及扩展,SO可以优先考虑数据模型,而忽略底层的DB-API切换,数据迁移更方便.

 

快速安装:

1

pip install --upgrade SQLAlchemy

 

定义结构:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

#!/usr/bin/env python

# -*- coding: utf-8 -*-

"""

#

# Authors: limanman

# OsChina: http://xmdevops.blog.51cto.com/

# Purpose:

#

"""

# 说明: 导入公共模块

from datetime import datetime

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy import (create_engine, MetaData, Table, Column, Integer, String, DateTime, Numeric,

                        ForeignKey, PrimaryKeyConstraint, ForeignKeyConstraint, UniqueConstraint)

# 说明: 导入其它模块

from constants import USERNAME, PASSWORD, HOSTNAME, HOSTPORT, DATABASE, CHARSSET, ALLDBURI

Base = declarative_base()

class Cookies(Base):

    __tablename__ = 'cookies'

    id = Column(Integer(), primary_key=True, autoincrement=True)

    cookie_name = Column(String(50), index=True)

    cookie_recipe_url = Column(String(255))

    cookie_sku = Column(String(55))

    quantity = Column(Integer())

    unit_cost = Column(Numeric(122))

说明: SQLAlchemy ORM与SQLAlchemy Core有些许不同,用户只用关注上层数据对象定义,之前我们还需要定义metadata容器对象让表对象引用它,而在ORM中只需要定义的表类继承自declarative_base实例基类则会自动关联表对象和metadata对象,而无需关注底层的实现.

注意: 要定义一个ORM的表类,需要满足至少四个条件,必须继承自declarative_base实例,必须包含一个__tablename__类属性,值为对应数据库中的表名,必须至少包含一个或是更多的列属性,必须至少有一个或更多列作为主键

 

索引约束:

1

2

3

4

5

6

7

8

9

class Cookies(Base):

    __tablename__ = 'cookies'

    __table_args__ = (PrimaryKeyConstraint('id'))

    id = Column(Integer(), autoincrement=True)

    cookie_name = Column(String(50), index=True)

    cookie_recipe_url = Column(String(255))

    cookie_sku = Column(String(55))

    quantity = Column(Integer())

    unit_cost = Column(Numeric(122))

说明: SQLAlchemy Core中可直接在定义Table时定义表级约束/索引,在SQLAlchemy ORM中也可以很方便实现,可通过设置表类的__table_args__ 类属性来设置多个索引和约束,值可为序列对象,PrimaryKeyConstraint, ForeignKeyConstraint, UniqueConstraint的用法和SQLAlchemy Core是一样的~

 

关系映射:

说明: 如上图,一个用户可以产生多个订单,所以在orders表中创建外键user_id关联users表,而每个订单又可以包含多个商品,且可以包含多个相同的商品最终还得有一个最终价格,所以在line_items中创建外键order_id关联orders,又因为每个商品有具体详情所以在line_items中创建外键cookie_id关联cookies

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

#!/usr/bin/env python

# -*- coding: utf-8 -*-

"""

#

# Authors: limanman

# OsChina: http://xmdevops.blog.51cto.com/

# Purpose:

#

"""

# 说明: 导入公共模块

from datetime import datetime

from sqlalchemy.orm import relationship, backref

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy import (create_engine, Column, Integer, String, DateTime, Numeric, ForeignKey)

# 说明: 导入其它模块

from constants import USERNAME, PASSWORD, HOSTNAME, HOSTPORT, DATABASE, CHARSSET, ALLDBURI

Base = declarative_base()

class Users(Base):

    __tablename__ = 'users'

    id = Column(Integer(), primary_key=True, autoincrement=True)

    customer_number = Column(Integer(), autoincrement=True)

    username = Column(String(15), unique=True, nullable=False)

    email_address = Column(String(255), nullable=False)

    phone = Column(String(20), nullable=False)

    password = Column(String(25), nullable=False)

    created_on = Column(DateTime(), default=datetime.now)

    updated_on = Column(DateTime(), default=datetime.now, onupdate=datetime.now)

class Orders(Base):

    __tablename__ = 'orders'

    id = Column(Integer(), primary_key=True, autoincrement=True)

    user_id = Column(Integer(), ForeignKey('Users.id'), nullable=False)

    user = relationship('Users', backref=backref('orders', order_by=id))

class LineItems(Base):

    __tablename__ = 'line_items'

    id = Column(Integer(), primary_key=True, autoincrement=True)

    quantity = Column(Integer())

    extended_cost = Column(Numeric(122))

    order_id = Column(Integer(), ForeignKey('Orders.id'), nullable=False)

    cookie_id = Column(Integer(), ForeignKey('Cookies.id'), nullable=False)

    order = relationship('Orders', backref=backref('line_items', order_by=id))

    cookies = relationship('Cookies', backref=backref('line_item', uselist=False))

class Cookies(Base):

    __tablename__ = 'cookies'

    id = Column(Integer(), primary_key=True, autoincrement=True)

    cookie_name = Column(String(50), index=True)

    cookie_recipe_url = Column(String(255))

    cookie_sku = Column(String(55))

    quantity = Column(Integer())

    unit_cost = Column(Numeric(122))

if __name__ == '__main__':

    # 第一步: 创建引擎对象

    engine = create_engine(ALLDBURI, echo=True, pool_recycle=3600)

    # 第二步: 创建元数据对象

    Base.metadata.create_all(bind=engine)

说明: 如上简单演示了SQLAlchemy ORM常见建表过程,和SQLAlchemy Core不同,它强制通过relationship和backref来为关联表类增加互相指向的类属性,relationship的第一个参数为要关联的表类名称,常用的还有backref,通过backref创建,第一个参数为支持另一个表反向访问的属性名,剩余的relationship和backref的参数一样的,如上的uselist=False表示反向引用时是one2one模式,更多参数可以参考源码实例Lib/site-packages/sqlalchemy/orm/relationships.py

 

登录乐搏学院官网

或关注我们的官方微博,还有更多惊喜哦~

转载于:https://my.oschina.net/learnbo/blog/844328

你可能感兴趣的文章
CCCC L1-039. 古风排版【图形输出/循环控制行列/模拟/细节】
查看>>
POJ 1182 食物链 【带权并查集/补集法】
查看>>
V字形
查看>>
Flask学习笔记(3)-数据库迁移
查看>>
Hbase常用操作
查看>>
一行命令学会全基因组关联分析(GWAS)的meta分析
查看>>
第二阶段冲刺——six
查看>>
模块封装代码
查看>>
《Machine Learning》(第一章)序章
查看>>
【右键禁用U盘的小技巧】
查看>>
执行sql语句后的数据处理api
查看>>
jquery $.each的用法
查看>>
Python --元组与列表的差异
查看>>
PHP TP增删改
查看>>
VMware虚拟机与主机联通及配置上网
查看>>
single-row function和muti-row function
查看>>
keepalived
查看>>
意向锁
查看>>
线性规划
查看>>
常见错误分析-笔记
查看>>