我想将记录插入到3个表中。代码如下。
sessionDB.autocommit = False
# insert Address
addrRecord = Addres(street=street, city=city, zip_code=zip_code)
sessionDB.add(addrRecord)
sessionDB.flush()
# insert customer
cRecord = Customer(email=email, passwords=password, kind='individual', aID=addrRecord.aID)
sessionDB.add(cRecord)
sessionDB.flush()
# insert Hcustomer
homeCRecord = HomeCu(cID=cRecord.cID, fname=first_name, lname=last_name, age=age, marriage=marriage, remain=remain)
sessionDB.add(homeCRecord)
print (addrRecord.city, cRecord.kind, homeCRecord.lname)
sessionDB.commit()
Addres的密钥是aID,而客户的密钥是cID。它们是自动递增的。在客户中,有一个受Addres的aID约束的外键。 HomeCu的外键cID受客户的cID约束。 所以我想依次插入Addres,Customer,HomeCu,在此期间我必须不执行提交就获取密钥。我使用flush()获取密钥,但是提交操作失败。为什么在提交操作期间,诸如email,password之类的值变为None,但打印语句运行得很好?我已经为此奋斗了几个小时。您的帮助将不胜感激。非常感谢!!!
这是错误日志:
回溯(最近通话最近):
文件
“ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py”, 第1193行,在_execute_context上下文中)
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/default.py”, 在do_execute cursor.execute(statement,parameters)中的509行文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/cursors.py”, 第170行,执行结果= self._query(query)
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/cursors.py”, _query conn.query(q)中的第328行
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py”, 查询self._affected_rows =第516行 self._read_query_result(unbuffered = unbuffered)
文件
“ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py”, 第727行,位于_read_query_result result.read()
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py”, 第1066行,处于读取first_packet = self.connection._read_packet()文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py”, 第683行,位于_read_packet包中。check_error()
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/protocol.py”, 第220行,在check_error err.raise_mysql_exception(self._data)文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/err.py”, 第109行,在raise_mysql_exception中引发errorclass(errno,errval) pymysql.err.IntegrityError:(1048,“列'email'不能为空”) 上面的异常是以下异常的直接原因: 追溯(最近一次通话):
文件“ /卷/代码/上一页
Content / grad / 2018_2019 / db / final / app / modelController.py“,第164行,在 registerIndividual('534 Tilling St','New York','15233', '123@qq.com','123','Zimo','Tang',23、755.23、0)
文件
“ /卷/代码/上一页
Content / grad / 2018_2019 / db / final / app / modelController.py“,第129行,在 registerIndividual sessionDB.commit()
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py”, 第943行,在commit self.transaction.commit()
中 文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py”, 第467行,提交self._prepare_impl()
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py”, _prepare_impl self.session.flush()中的第447行
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py”, 第2254行,在冲洗self._flush(objects)
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py”, 第2381行,在_flush transaction.rollback(_capture_exception = True)文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py”, 第66行,位于退出 compat.reraise(exc_type,exc_value,exc_tb)
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/compat.py”, 第249行,在提高筹款价值中
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py”, _flush flush_context.execute()中的第2345行
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py”, 第395行,在execute rec.execute(self)
中 文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py”, 第560行,在执行uow
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py”, 第181行,在save_obj映射器中,表,插入)
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py”, 第830行,在_emit_insert_statements中,execute(statement, 多参数)
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py”, 第948行,在执行中返回meth(self,multiparams,params)
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/sql/elements.py”, 第269行,在_execute_on_connection中返回 connection._execute_clauseelement(自己,多参数,参数)
文件
“ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py”, 第1060行,在_execute_clauseelement编译的sql中,distilled_params文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py”, _execute_context上下文中的第1200行)
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py”, _handle_dbapi_exception exc_info中的第1413行
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/compat.py”, 第265行,raise_from_cause reraise(type(exception),exception, tb = exc_tb,原因=原因)
文件
“ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/compat.py”, 第248行,在提高筹码中。with_traceback(tb)
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py”, 第1193行,在_execute_context上下文中)
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/default.py”, 在do_execute cursor.execute(statement,parameters)中的509行文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/cursors.py”, 第170行,执行结果= self._query(query)
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/cursors.py”, _query conn.query(q)中的第328行
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py”, 查询self._affected_rows =第516行 self._read_query_result(unbuffered = unbuffered)
文件
“ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py”, 第727行,位于_read_query_result result.read()
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py”, 第1066行,处于读取first_packet = self.connection._read_packet()文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py”, 第683行,位于_read_packet包中。check_error()
文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/protocol.py”, 第220行,在check_error err.raise_mysql_exception(self._data)文件 “ /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/err.py”, 第109行,在raise_mysql_exception中引发errorclass(errno,errval) sqlalchemy.exc.IntegrityError:(pymysql.err.IntegrityError)(1048, “列'email'不能为空”)[SQL:'INSERT INTO customer (
cID
,电子邮件,密码,种类,aID
)值(%(cID)s,%(email)s, %(passwords)s,%(kind)s,%(aID)s)'] [参数:{'cID':31, 'email':无,'passwords':无,'kind':无,'aID':无} (此错误的背景位于:http://sqlalche.me/e/gkpj)以退出代码1完成的过程
答案 0 :(得分:0)
addrRecord = sessionDB.query(Addres).filter(Addres.street == street).filter(Addres.city == city) \
.filter(Addres.zip_code == zip_code).first()
cRecord = Customer(email=email, passwords=password, kind='individual', aID=addrRecord.aID)
homeCRecord = HomeCu(fname=first_name, lname=last_name, age=age, marriage=marriage, remain=remain)
homeCRecord.customer = cRecord
sessionDB.add(cRecord)
sessionDB.add(homeCRecord)
我最终没有使用冲洗。我认为多次使用冲洗会导致错误。我在实例中添加关系。希望这可以帮助某人。