问题来源:
封装好远程数据库查询模块
第一次调用,传入参数
第二次调用,传入参数,无效,返回的值仍然时第一次调用的值
问题代码:
createtables文件下代码:
class Design(Base):
__tablename__='design'
design_id=Column(Integer,primary_key=True)
teacher_id = Column(Integer, ForeignKey('teacher.teacher_id'), primary_key=True)
student_id = Column(Integer, ForeignKey('student.student_id'),primary_key=True)
name=Column(String(50))
(忘了teacher_id和student_id是主键时错误,还是非主键时错误了)
connect文件下代码:
from sqlalchemy import create_engine, or_
from sqlalchemy.orm import scoped_session, sessionmaker, session
from SQLAlchemy import createtables
import random as r
from SQLAlchemy import choicedemo
#此为远程数据库连接信息
db_connect_string='mysql://账号:密码@ip:3306/数据库名称?charset=utf8'
engine=create_engine(db_connect_string)
SessionType=scoped_session(sessionmaker(bind=engine,expire_on_commit=False))
def GetSession():
return SessionType()
from contextlib import contextmanager
@contextmanager
def session_scope():
session=GetSession()
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
class Get():
def Design(self,design_id=None,teacher_id=None, student_id=None, name=None):
with session_scope() as session:
return session.query(createtables.Design).filter(
or_(createtables.Design.design_id == design_id,createtables.Design.teacher_id == teacher_id, createtables.Design.student_id == student_id
, createtables.Design.name == name)).first()
当调用design方法的时候就会出现错误。
问题原因:
不详。
解决办法:
定义数据表的时候,将teacher,student主键去除:
class Design(Base):
__tablename__='design'
design_id=Column(Integer,primary_key=True)
teacher_id = Column(Integer, ForeignKey('teacher.teacher_id'))
student_id = Column(Integer, ForeignKey('student.student_id'))
name=Column(String(50))
暂时性解决办法。
如果有懂的,希望评论解惑,谢谢
版权声明:本文为shashouleilei原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。