Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 覆盖Sqlalchemy中的整行_Python_Sqlalchemy - Fatal编程技术网

Python 覆盖Sqlalchemy中的整行

Python 覆盖Sqlalchemy中的整行,python,sqlalchemy,Python,Sqlalchemy,我一直在阅读sqlalchemy中的方法,似乎有很多,包括最常见的.update({…})。但是,我不想指定要更新的字段,因为我只想覆盖与主键关联的整行。做这件事的好方法是什么 我能想到的一种方法是删除匹配的行,然后提交新的行。但是,我不清楚这样做是否会产生任何奇怪的后果,因为我的主键是autogen而不是用户定义的id=Column(Integer,primary\u key=True,autoincrement=True)我想您可以创建一个充满空值的dict,然后在添加新值后提交该行: 首先

我一直在阅读sqlalchemy中的方法,似乎有很多,包括最常见的
.update({…})
。但是,我不想指定要更新的字段,因为我只想覆盖与主键关联的整行。做这件事的好方法是什么


我能想到的一种方法是删除匹配的行,然后提交新的行。但是,我不清楚这样做是否会产生任何奇怪的后果,因为我的主键是autogen而不是用户定义的
id=Column(Integer,primary\u key=True,autoincrement=True)

我想您可以创建一个充满空值的dict,然后在添加新值后提交该行:

首先,为表中的每一列创建一个充满空值的字典:

some_table = db.Table('exact_table_name', metadata, autoload=True)
existing_fields = {column: " " for column in some_table.c}
然后添加要更新的值,并创建查询

existing_fields.update(**known_update_dict)
query = some_table.update()
# just adding some where-condition here... 
query = query.where(getattr(some_table.c, "primary_key") == 99)
query.values(**existing_fields)

然后执行它。有点费解,但我也不知道更好的方法——我认为没有专门的函数或查询词来实现这一点。

您的数据是什么格式的?您有您的
型号的实例吗?您是否有带有新值的
dict