SQLAlchemy in Python as an ORM or more famous, the following is the establishment of a few simple models, fully and flask data persistence layer separation.
1 #Coding:utf82 fromSQLAlchemyImportColumn, String, Integer, DateTime, BigInteger, Numeric, ForeignKey, Smallinteger, Create_engine3 fromSqlalchemy.ext.declarativeImportDeclarative_base4 fromSqlalchemy.ormImportcreate_session, Relationship5 6Base =declarative_base ()7_db_session =None8 9 Ten defget_engine (): One returnCreate_engine ('Mysql+pymysql://root:[email Protected]/wms?charset=utf8', echo=True) A - - defget_session (): the if_db_session isNone: -Engine =Get_engine () - returncreate_session (Engine) - Else: + return_db_session - + A defCreate_all (): atEngine =Get_engine () - Base.metadata.create_all (Engine) - - - defDrop_all (): -Engine =Get_engine () in Base.metadata.drop_all (Engine) - to + classRole (Base): - __tablename__='A_role' theCode = Column (String), primary_key=True) *Label = Column (String (50)) $ Panax Notoginseng - classUser (Base): the __tablename__='A_user' +id = Column (BigInteger, primary_key=True) ACode = Column (String), Nullable=false, Index=true, unique=True) theLabel = Column (String (50)) +PWD = Column (String (50)) -Create_date =Column (DateTime) $Memo = Column (String (50)) $Role_code = Column (String), ForeignKey ('A_role.code')) -Role = Relationship ('Role', backref='Users') - the - classSupplierclass (Base):Wuyi __tablename__='A_supplier_class' theCode = Column (String (), primary_key=True) -Label = Column (String (50)) Wu - About classSupplier (Base): $ __tablename__='A_supplier' -id = Column (BigInteger, primary_key=True) -Code = Column (String (), index=True) -Label = Column (String (50)) ATel = Column (String (30)) +Address = Column (String (50)) theContacts = Column (String (30)) -Level =Column (Smallinteger) $Create_date =Column (DateTime) theMemo = Column (String (50)) theClass_code = Column (String (), ForeignKey ('A_supplier_class.code')) theSupplier_class = Relationship ('Supplierclass', backref='Suppliers') the - in classWarehouse (Base): the __tablename__='A_warehouse' theCode = Column (String (), primary_key=True) AboutLabel = Column (String (50)) theAddress = Column (String (50)) theCreate_date =Column (DateTime) theMemo = Column (String (50)) +manager_id = Column (BigInteger, ForeignKey ('a_user.id')) -Manager = Relationship ('User') the Bayi the classLocationclass (Base): the __tablename__='A_location_class' -Code = Column (String (), primary_key=True) -Label = Column (String (50)) the the the classLocation (Base): the __tablename__='a_location' -id = Column (BigInteger, primary_key=True) theCode = Column (String (), index=True) theLabel = Column (String (50)) theMax_qty = Column (Numeric (10, 2))94Create_date =Column (DateTime) theMemo = Column (String (50)) theWarehouse_code = Column (String (), ForeignKey ('A_warehouse.code')) themanager_id = Column (BigInteger, ForeignKey ('a_user.id'))98Class_code = Column (String (), ForeignKey ('A_location_class.code')) AboutManager = Relationship ('User') -Warehouse = Relationship ('Warehouse')101Location_class = Relationship ('Locationclass')102 103 104 classItemclass (Base): the __tablename__='A_item_class'106Code = Column (String (), primary_key=True)107Label = Column (String (50))108 109 the classItem (Base):111 __tablename__='A_item' theid = Column (BigInteger, primary_key=True)113Code = Column (String (), index=true)#Item Code theLabel = Column (String (100))#name theType = Column (Smallinteger)#0 Raw materials, 1 semi-finished products, 20% products theClass_code = Column (String (), ForeignKey ('A_item_class.code'))117location_id = Column (BigInteger, ForeignKey ('a_location.id'))118Item_class = Relationship ('Itemclass')#Material Classification119Location = Relationship (' Location')#Recommended Location -Safety_stock = Column (Numeric (10, 2))#Safety Stock121MPQ = Column (Numeric (10, 2))#Minimum Packing quantity122Create_date =Column (DateTime)123Memo = Column (String (50))124 the 126 #Drop_all ()127 #Create_all ()
SQLAlchemy Data Model