Commit 995e2932 authored by xianyang's avatar xianyang

代码优化

parent 07fab79d
Pipeline #413 canceled with stages
......@@ -3,6 +3,7 @@
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/fast_api" />
<excludeFolder url="file://$MODULE_DIR$/fs-env" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
......
import pymysql
pymysql.install_as_MySQLdb()
\ No newline at end of file
......@@ -24,13 +24,13 @@ def create_user(db: Session, user: schemas.UserCreate):
return db_user
def get_items(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.Item).offset(skip).limit(limit).all()
def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int):
db_item = models.Item(**item.dict(), owner_id=user_id)
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item
# def get_items(db: Session, skip: int = 0, limit: int = 100):
# return db.query(models.Item).offset(skip).limit(limit).all()
#
#
# def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int):
# db_item = models.Item(**item.dict(), owner_id=user_id)
# db.add(db_item)
# db.commit()
# db.refresh(db_item)
# return db_item
......@@ -8,19 +8,19 @@ class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
email = Column(String, unique=True, index=True)
hashed_password = Column(String)
email = Column(String(50), unique=True, index=True)
hashed_password = Column(String(100))
is_active = Column(Boolean, default=True)
items = relationship("Item", back_populates="owner")
class Item(Base):
__tablename__ = "items"
id = Column(Integer, primary_key=True, index=True)
title = Column(String, index=True)
description = Column(String, index=True)
owner_id = Column(Integer, ForeignKey("users.id"))
owner = relationship("User", back_populates="items")
\ No newline at end of file
# class Item(Base):
# __tablename__ = "items"
#
# id = Column(Integer, primary_key=True, index=True)
# title = Column(String, index=True)
# description = Column(String, index=True)
# owner_id = Column(Integer, ForeignKey("users.id"))
#
# owner = relationship("User", back_populates="items")
\ No newline at end of file
# encoding:utf-8
'''
创建数据库连接引擎
'''
"""创建数据库连接引擎"""
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
......@@ -19,9 +17,10 @@ SQLALCHEMY_DATABASE_URL = f'sqlite:///{modul_path}/sql_app.db'
# SQLALCHEMY_DATABASE_URL = f'mysql://{env.DATABASE_USER}:{env.DATABASE_PWD}@{env.DATABASE_URI}'
# connect_args={'check_same_thread': False} 仅仅在使用 sqlite数据库时有用
engine = create_engine(
SQLALCHEMY_DATABASE_URL, encoding='utf-8', echo=True, connect_args={'check_same_thread': False}
)
# engine = create_engine(
# SQLALCHEMY_DATABASE_URL, encoding='utf-8', echo=True, connect_args={'check_same_thread': False}
# )
engine = create_engine('mysql+mysqldb://data_center:KCMBfAjeJhbJXsSe@43.138.132.9:3398/fj-data-center')
# 数据库 session 类,用于创建 session 实例
# autoflush 是指发送数据库语句到数据库,但数据库不一定执行写入到磁盘
......
......@@ -5,7 +5,6 @@ fastapi==0.89.1
greenlet==2.0.2
h11==0.14.0
idna==3.4
jose==1.0.0
passlib==1.7.4
pydantic==1.10.4
sniffio==1.3.0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment