sqlalchemy数据模型

时间:2023-03-08 17:44:04

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()