SQLAlchemy: Modification of detached object

让人想犯罪 __ 提交于 2019-11-29 22:24:35
zzzeek

this case is available using the make_transient() helper function:

inst = session.query(Model).first()
session.expunge(inst)

make_transient(inst)
inst.id = None
session.add(inst)
session.flush()
print inst.id #New ID
def duplicate(self):
    arguments = dict()
    for name, column in self.__mapper__.columns.items():
        if not (column.primary_key or column.unique):
            arguments[name] = getattr(self, name)
    return self.__class__(**arguments)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!