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. 网络电视精灵~分析~~~~~~简单工厂模式,继承和多态,解析XML文档,视频项目

    小总结: 所用技术: 01.C/S架构,数据存储在XML文件中 02.简单工厂模式 03.继承和多态 04.解析XML文档技术 05.深入剖析内存中数据的走向 06.TreeView控件的使用 核心: ...

  2. simple_html_dom配合snoopy使用

    https://github.com/samacs/simple_html_dom Snoopy的特点是“大”和“全”,一个fetch什么都采到了,可以作为采集的第一步.接下来就需要用simple_h ...

  3. XSHELL使用隧道

    线上系统中,搭建了一个elasticsearch环境,想要访问页面,发现环境的内网中没有windows机器,无法使用浏览器来直接进行web页面的访问,于是直接使用了XSELL中强大的功能"隧 ...

  4. 炫酷的Linux终端命令大全-1

    1. 命令行日常快捷键. CTRL + U            ------------------------------- 剪切光标前的内容 CTRL + K             ----- ...

  5. 探索软件工程道路上的我 IV (Θ∀Θ#)

    开发语言:Java 开发工具:UltraEdit 小伙伴博客:http://www.cnblogs.com/hyating/ github地址:https://github.com/JUNYU217/ ...

  6. MAC下如何显示隐藏文件

    1.在终端上输入以下命令 defaults write com.apple.finder AppleShowAllFiles -bool true 2.重新启动Finder Command + Opt ...

  7. css性能优化

    1.前端 1.1.减少http请求次数: 1.1.1先了解下HTTP对性能的影响,HTTP是浏览器和服务器通过Interet进行相互通信的协议.HTTP是一种客服端/服务器协议,有请求和响应构成. 浏 ...

  8. 项目用到的icarouls类和UIEffectDesignerView类,菜单技巧,构思(金方圆)

    // //  MenuHomeViewController.m //  HFYS // //  Created by Showsoft_002 on 13-8-14. //  Copyright (c ...

  9. jdbc mysql写入中文乱码解决

    一. 问题 数据库编码:utf8 mysql> create database dbnameDEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ...

  10. response 设置头的类型 (转)

    Response.ContentType 详细列表 不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式.代码如: <% r ...