课程名称:当代数据库管理系统 | 年级 :2020级 | 上机实践成绩: |
---|---|---|
指导教师 :高明 | 姓名 :杨舜、姚嘉和 | 学号 :10205501415、10205501436 |
上机实践名称 :BookStore | 上机实践日期:2022.11.28 —— 2022.12.10 | |
上机实践编号 : | 组号 :21 | 上机实践时间:2022.11.28 —— 2022.12.10 |
分析demo中/be/model/store.py中创建数据库表的sql语句可知原有数据库的结构的ER图大致如下
有上述ER图可以得到原有数据库表如下
user表:
user_id | password | balance | token | terminal |
---|---|---|---|---|
主键为user_id |
store表:
store_id | stock_level |
---|---|
主键为store_id |
store_book表:
store_id | book_id | book_info | stock_level |
---|---|---|---|
主键为联合主键(store_id,book_id) |
user_store表:
user_id | store_id |
---|---|
外键为(user_id,store_id) |
new_order表:
order_id | user_id | store_id |
---|---|---|
主键为(order_id) |
new_order_detail表:
oeder_id | book_id | count | price |
---|---|---|---|
主键为联合主键(order_id,book_id) |
class con:
def connect():
'''Returns a connection and a metadata object'''
# We connect with the help of the PostgreSQL URL
url = 'postgresql://stu10205501415:Stu10205501415@dase-cdms-2022-pub.pg.rds.aliyuncs.com:5432/stu10205501415'
# The return value of create_engine() is our connection object
con = create_engine(url, client_encoding='utf8')
# We then bind the connection to MetaData()
meta = MetaData(bind=con)
return con, meta
class User(Base):
__tablename__ = 'user'
user_id = Column(TEXT, primary_key=True, comment="主键")
password = Column(TEXT, nullable=False, comment="密码")
balance = Column(Integer, nullable=False, comment="")
token = Column(TEXT, comment="缓存的令牌")
terminal = Column(TEXT, comment="终端代码")
class Store(Base):
__tablename__ = 'store'
store_id = Column(TEXT, primary_key=True,comment="主键")
stock_level = Column(Integer, comment = "货存")
class Store_Book(Base):
__tablename__ = 'store_book'
store_id = Column(TEXT, comment="主键")
book_id = Column(TEXT, comment="主键")
book_info = Column(TEXT, comment="书籍信息")
stock_level = Column(Integer, comment = "货存")
__table_args__ = (
PrimaryKeyConstraint('store_id', 'book_id'),
)
class User_Store(Base):
__tablename__ = 'user_store'
id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
fk_user_id = Column(
TEXT,
ForeignKey(
"user.user_id",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
comment="user外键"
)
fk_store_id = Column(
TEXT,
ForeignKey(
"store.store_id",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
comment="store外键"
)
# 多对多关系的中间表必须使用联合唯一约束,防止出现重复数据
__table_args__ = (
UniqueConstraint("fk_user_id", "fk_store_id"),
)
class New_Order(Base):
__tablename__ = 'new_order'
order_id = Column(TEXT, primary_key = True, comment = '订单id')
fk_user_id = Column(
TEXT,
ForeignKey(
"user.user_id",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
comment="user外键"
)
fk_store_id = Column(
TEXT,
ForeignKey(
"store.store_id",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
comment="store外键"
)
class New_Order_Detail(Base):
__tablename__ = 'new_order_detail'
order_id = Column(TEXT, comment='订单id')
book_id = Column(TEXT, comment='订单书籍')
count = Column(Integer, comment='购买书籍数')
price = Column(Integer, comment='单价')
__table_args__ = (
PrimaryKeyConstraint('order_id','book_id'),
)
engine, meta = con.connect()
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
app.register_blueprint(auth.bp_auth)
app.register_blueprint(seller.bp_seller)
app.register_blueprint(buyer.bp_buyer)
from model import postgreSQLORM
from model.postgreSQLORM import User, Store_Book, User_Store
class DBConn:
def __init__(self):
return
# self.conn = store.get_db_conn()
def user_id_exist(self, user_id):
row = postgreSQLORM.session.query(User).filter(User.user_id==user_id).one()
# cursor = self.conn.execute("SELECT user_id FROM user WHERE user_id = ?;", (user_id,))
# row = cursor.fetchone()
if row is None:
return False
else:
return True
def book_id_exist(self, store_id, book_id):
row = postgreSQLORM.session.query(Store_Book).filter(Store_Book.book_id==book_id and Store_Book.store_id==store_id).one()
# cursor = self.conn.execute("SELECT book_id FROM store WHERE store_id = ? AND book_id = ?;", (store_id, book_id))
# row = cursor.fetchone()
if row is None:
return False
else:
return True
def store_id_exist(self, store_id):
row = postgreSQLORM.session.query(User_Store).filter(User_Store.fk_store_id==store_id).one()
# cursor = self.conn.execute("SELECT store_id FROM user_store WHERE store_id = ?;", (store_id,))
# row = cursor.fetchone()
if row is None:
return False
else:
return True