sqlalchemy数据模型
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数据模型的更多相关文章
- Django 优秀资源大全
版权: https://github.com/haiiiiiyun/awesome-django-cn 转自:https://www.jianshu.com/p/38c4dd6d8e28 Awesom ...
- sqlalchemy学习
sqlalchemy官网API参考 原文作为一个Pythoner,不会SQLAlchemy都不好意思跟同行打招呼! #作者:笑虎 #链接:https://zhuanlan.zhihu.com/p/23 ...
- 20.Python笔记之SqlAlchemy使用
Date:2016-03-27 Title:20.Python笔记之SqlAlchemy使用 Tags:python Category:Python 作者:刘耀 博客:www.liuyao.me 一. ...
- 循序渐进Python3(十)-- 2 -- SqlAlchemy
ORM 对象关系映射(英语:Object Relation Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类 ...
- python【第十二篇下】操作MySQL数据库以及ORM之 sqlalchemy
内容一览: 1.Python操作MySQL数据库 2.ORM sqlalchemy学习 1.Python操作MySQL数据库 2. ORM sqlachemy 2.1 ORM简介 对象关系映射(英语: ...
- 使用SqlAlchemy时如何方便的取得dict数据、dumps成Json
使用Sqlalchemy可以方便的从数据库读取出python对象形式的数据(吐槽:说实话对象形式也没多方便,还不如我之前从关系型数据库直接读取出dict形式的数据用起来方便,具体参见我以前的文章htt ...
- tornado with MySQL, torndb, django model, SQLAlchemy ==> JSON dumped
现在,我们用torndo做web开发框架,用他内部机制来处理HTTP请求.传说中的非阻塞式服务. 整来整去,可谓之一波三折.可是,无论怎么样,算是被我做成功了. 在tornado服务上,采用三种数据库 ...
- sqlalchemy 映射的小例子
1.多张表映射到一个类 import pandas as pdfrom settings import DATABASESfrom sqlalchemy import create_engineimp ...
- flask SQLALchemy外键及约束
from flask import Flask,session from flask_sqlalchemy import SQLAlchemy import config app = Flask(__ ...
随机推荐
- git pull错误
1. Pull is not possible because you have unmerged files. 症状:pull的时候 $ git pull Pull is not possible ...
- 2014年6月份第3周51Aspx源码发布详情
基于知识树的多课程网络教学平台源码 2014-6-16 [VS2008]功能介绍:本平台是一个支持网上教学的网站,支持多个课程,教师可根据需要创建课程,进行课程结构.题库等的管理. 技术特色 ...
- [原]centos6.5系统可用yum源(32位)以及rpmforge
[10gen] name=10gen Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686 gpgche ...
- Jquery仿彩票更换数字动画效果
<script type="text/javascript" src="jquery-1.11.3.min.js"></script> ...
- 盲注----基于布尔的SQL盲注
构造逻辑判断常用字符串截取函数http://www.cnblogs.com/yyccww/p/6054569.html常用函数left(a,b) 从左侧截取a的前b位right(a,b) 从右侧截 ...
- selected 刷新页面后selected选中的值保持不表(thinkphp 从控制器assign 传值到js)
昨晚解决select 刷新页面以后选择的值保持不变,要想让seleted不变,有两种思路, 1,在提交表单的时候,将所选择的option的属性设为checked . 2.将option的value或者 ...
- IOS atomic与nonatomic,assign,copy与retain的定义和区别
IOS atomic与nonatomic,assign,copy与retain的定义和区别 atomic和nonatomic用来决定编译器生成的getter和setter是否为原子操作. ...
- svn/git的diff、patch
svn/git的diff.patch 前几天,正当我突突的写代码,企业微信嘀嘀一声响”在不,过来帮我看个bug”.本人一向助人为乐,高兴的冲了过去,然后就开始了一段长达1分钟的问题描述.很明显,此同学 ...
- 1、MVC和EF中的 Model First 和 Code First
准备:先引入MVC和EF的dll包 *命令方法:打开工具——库程序包管理器——程序包管理器控制台,选择自己的项目 a) Install-Package EntityFramework -Ver ...
- Cocos2dx对精灵的优化
cocos2dx针对游戏设计的不同方面会有不同的优化方案,可以对声音,对内存,对图片格式,对色彩等等进行优化.有关这些方面的方法请大家查找其他的文章.我今天要说的是如何对精灵进行优化,程序中我们用到的 ...