You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.3 KiB
52 lines
1.3 KiB
from sqlalchemy.orm import declarative_base |
|
from db import engine |
|
from sqlalchemy import Table, Column, Integer, String, Enum, DATE, MetaData |
|
|
|
Base = declarative_base() |
|
|
|
|
|
# 创建表结构 -- 声名式 |
|
class User(Base): |
|
__tablename__ = 'user' |
|
id = Column(Integer, primary_key=True) |
|
name = Column(String(32)) |
|
password = Column(String(64)) |
|
|
|
def __repr__(self): |
|
return "<%s name:%s>" % (self.id, self.name) |
|
|
|
|
|
class Student(Base): |
|
__tablename__ = 'student' |
|
id = Column(Integer, primary_key=True) |
|
name = Column(String(32), nullable=False) |
|
gender = Column(Enum('m', 'f'), nullable=False) |
|
date = Column(DATE, nullable=False) |
|
|
|
def __repr__(self): |
|
return f"<{self.id}-{self.name}-{self.gender}-{self.date}>" |
|
|
|
|
|
Base.metadata.create_all(engine) # 创建全部表 |
|
User.__table__.create(engine) # 创建单个表 |
|
|
|
# 创建表结构 -- 命令式 |
|
|
|
metadata = MetaData() # 生成metadata |
|
|
|
user = Table( |
|
'user', |
|
metadata, |
|
Column('id', Integer, primary_key=True), |
|
Column('name', String(32)), |
|
Column('password', String(64)) |
|
) |
|
|
|
student = Table( |
|
'student', |
|
metadata, |
|
Column('id', Integer, primary_key=True), |
|
Column('name', String(32), nullable=False), |
|
Column('gender', Enum('m', 'f'), nullable=False), |
|
Column('date', DATE, nullable=False) |
|
)
|
|
|