简体   繁体   English

Python session.commit()更新现有行,而不是插入新行

[英]Python session.commit() updates existing row instead of inserting a new one

I am having an issue where I am looping through several rows of a CSV file and I need to insert a new row to my mySQL table at the end of every iteration of the for loop. 我遇到一个问题,即我要遍历CSV文件的几行,并且需要在for循环的每次迭代结束时在mySQL表中插入新行。 However, for some reason, sqlAlchemy is updating the previously existing row instead of inserting a new one. 但是,由于某种原因,sqlAlchemy将更新先前存在的行,而不是插入新行。

I cannot share my specific code because my work doesn't allow me to, but it does the following 我无法共享我的特定代码,因为我的工作不允许我这样做,但是它可以执行以下操作

for row in csv_file:
    do stuff
    try:
      session.commit()
    except Exception as e:
      logging.info(e)
      session.rollback()

Any ideas as to what I might be doing wrong? 关于我可能做错了什么的任何想法? Any help is much appreciated! 任何帮助深表感谢!

EDIT: Just figured out what I was doing wrong, I wasn't instantiating my SQL model class at the beginning of every iteration. 编辑:只是弄清楚我做错了什么,我没有在每次迭代的开始实例化我的SQL模型类。 It works now 现在可以使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM