sqlalchemy在python里作为orm还是比较有名气的,以下是建立的几个简单模型,完全可和flask的数据持久层分离。

 # coding: utf8
from sqlalchemy import Column, String, Integer, DateTime, BigInteger, Numeric, ForeignKey, SmallInteger, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import create_session, relationship Base = declarative_base()
_db_session = None def get_engine():
return create_engine('mysql+pymysql://root:1234@localhost/wms?charset=utf8', echo=True) def get_session():
if _db_session is None:
engine = get_engine()
return create_session(engine)
else:
return _db_session def create_all():
engine = get_engine()
Base.metadata.create_all(engine) def drop_all():
engine = get_engine()
Base.metadata.drop_all(engine) class Role(Base):
__tablename__ = 'a_role'
code = Column(String(20), primary_key=True)
label = Column(String(50)) class User(Base):
__tablename__ = 'a_user'
id = Column(BigInteger, primary_key=True)
code = Column(String(20), nullable=False, index=True, unique=True)
label = Column(String(50))
pwd = Column(String(50))
create_date = Column(DateTime)
memo = Column(String(50))
role_code = Column(String(20), ForeignKey('a_role.code'))
role = relationship('Role', backref='users') class SupplierClass(Base):
__tablename__ = 'a_supplier_class'
code = Column(String(30), primary_key=True)
label = Column(String(50)) class Supplier(Base):
__tablename__ = 'a_supplier'
id = Column(BigInteger, primary_key=True)
code = Column(String(30), index=True)
label = Column(String(50))
tel = Column(String(30))
address = Column(String(50))
contacts = Column(String(30))
level = Column(SmallInteger)
create_date = Column(DateTime)
memo = Column(String(50))
class_code = Column(String(30), ForeignKey('a_supplier_class.code'))
supplier_class = relationship('SupplierClass', backref='suppliers') class Warehouse(Base):
__tablename__ = 'a_warehouse'
code = Column(String(30), primary_key=True)
label = Column(String(50))
address = Column(String(50))
create_date = Column(DateTime)
memo = Column(String(50))
manager_id = Column(BigInteger, ForeignKey('a_user.id'))
manager = relationship('User') class LocationClass(Base):
__tablename__ = 'a_location_class'
code = Column(String(30), primary_key=True)
label = Column(String(50)) class Location(Base):
__tablename__ = 'a_location'
id = Column(BigInteger, primary_key=True)
code = Column(String(30), index=True)
label = Column(String(50))
max_qty = Column(Numeric(10, 2))
create_date = Column(DateTime)
memo = Column(String(50))
warehouse_code = Column(String(30), ForeignKey('a_warehouse.code'))
manager_id = Column(BigInteger, ForeignKey('a_user.id'))
class_code = Column(String(30), ForeignKey('a_location_class.code'))
manager = relationship('User')
warehouse = relationship('Warehouse')
location_class = relationship('LocationClass') class ItemClass(Base):
__tablename__ = 'a_item_class'
code = Column(String(30), primary_key=True)
label = Column(String(50)) class Item(Base):
__tablename__ = 'a_item'
id = Column(BigInteger, primary_key=True)
code = Column(String(30), index=True) # 物料代码
label = Column(String(100)) # 品名
type = Column(SmallInteger) # 0原材料,1半成品,2成品
class_code = Column(String(30), ForeignKey('a_item_class.code'))
location_id = Column(BigInteger, ForeignKey('a_location.id'))
item_class = relationship('ItemClass') # 物料分类
location = relationship('Location') # 推荐库位
safety_stock = Column(Numeric(10, 2)) # 安全库存
mpq = Column(Numeric(10, 2)) # 最小包装数量
create_date = Column(DateTime)
memo = Column(String(50)) # drop_all()
# create_all()

sqlalchemy数据模型的更多相关文章

  1. Django 优秀资源大全

    版权: https://github.com/haiiiiiyun/awesome-django-cn 转自:https://www.jianshu.com/p/38c4dd6d8e28 Awesom ...

  2. sqlalchemy学习

    sqlalchemy官网API参考 原文作为一个Pythoner,不会SQLAlchemy都不好意思跟同行打招呼! #作者:笑虎 #链接:https://zhuanlan.zhihu.com/p/23 ...

  3. 20.Python笔记之SqlAlchemy使用

    Date:2016-03-27 Title:20.Python笔记之SqlAlchemy使用 Tags:python Category:Python 作者:刘耀 博客:www.liuyao.me 一. ...

  4. 循序渐进Python3(十)-- 2 -- SqlAlchemy

    ORM             对象关系映射(英语:Object Relation Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类 ...

  5. python【第十二篇下】操作MySQL数据库以及ORM之 sqlalchemy

    内容一览: 1.Python操作MySQL数据库 2.ORM sqlalchemy学习 1.Python操作MySQL数据库 2. ORM sqlachemy 2.1 ORM简介 对象关系映射(英语: ...

  6. 使用SqlAlchemy时如何方便的取得dict数据、dumps成Json

    使用Sqlalchemy可以方便的从数据库读取出python对象形式的数据(吐槽:说实话对象形式也没多方便,还不如我之前从关系型数据库直接读取出dict形式的数据用起来方便,具体参见我以前的文章htt ...

  7. tornado with MySQL, torndb, django model, SQLAlchemy ==> JSON dumped

    现在,我们用torndo做web开发框架,用他内部机制来处理HTTP请求.传说中的非阻塞式服务. 整来整去,可谓之一波三折.可是,无论怎么样,算是被我做成功了. 在tornado服务上,采用三种数据库 ...

  8. sqlalchemy 映射的小例子

    1.多张表映射到一个类 import pandas as pdfrom settings import DATABASESfrom sqlalchemy import create_engineimp ...

  9. flask SQLALchemy外键及约束

    from flask import Flask,session from flask_sqlalchemy import SQLAlchemy import config app = Flask(__ ...

随机推荐

  1. 如何修复Outlook 2007源文件.PST及性能优化补丁

    kb961752 微软发布了改善 Outlook 2007 个人数据文件性能的补丁,该补丁未来将会集成于 SP2 中,但是现在对于SP1用户可以提前得到它.   获取地址: http://suppor ...

  2. oracle:ORACLE 实际返回的行数超出请求的行数

    写的存储过程,执行后一直报实际返回的行数超出请求的行数的错误. 原因:select prdt_id into prdt_id from.... 两个变量名称相同造成的..哎  第一个变量换成大写..问 ...

  3. Spring MVC学习笔记--认识SpringMVC

    Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这样的 Web 框 ...

  4. iOS 渐变进度条

    #import <UIKit/UIKit.h> @interface JianBianView : UIView //为了增加一个表示进度条的进行,可们可以使用mask属性来屏蔽一部分 @ ...

  5. vue学习笔记

    来公司以后就一直在用vue框架,不管是业务代码,还是做vue组件.关于vue有一些点是文档中没有提及的,记录一下以便以后查询- 一.Vue的特点 新一代 Vue.js 框架非常关注如何用极少的外部特性 ...

  6. Windows平台下PHP环境搭建

    在Windows平台上搭建PHP的开发环境可以下载WAMP(Windows.Apache.MySQL.PHP的首字母缩写)集成化安装包.这样就不需要单独安装Apache.MySQL和PHP了. 这款软 ...

  7. ReportViewer改变图表类型

    /// <summary>    /// 切换成柱状图    /// </summary>    /// <param name="sender"&g ...

  8. C++ list size()所想到的事情

      effective STL 某个Item里重点提出了 list.size()是一个O(n)的效率  当时只是记下来了  后面看了csdn有人在实际工程上遇到坑了  我近来闲来无事 把STL的lis ...

  9. BT协议分析(1)—1.0协议

    简述 BT下载是采用P2P的下载方式,下载的大致形式采用如下图所示,处于图示中心的称为Tracker服务器,其余称为Peer.   缺点 1.资源的安全性 2.资源的实效性(没有上传者则BT也将失效) ...

  10. 使用Burpsuite抓取手机APP的HTTPS数据

    1.所需条件 · 手机已经获取root权限 · 手机已经成功安装xposed框架 · 电脑一台 2.详细步骤 2.1 在手机上面安装xposed JustTrustMe JustTrustMe是一个去 ...