我正在使用SQLAlchemy和Postgres,需要创建"外部ID"这取决于Postgres生成的(顺序)id。代码如下所示:
# session is passed to the constructor as described in http://docs.sqlalchemy.org/en/latest/orm/session_basics.html#when-do-i-construct-a-session-when-do-i-commit-it-and-when-do-i-close-it
def make_merchant(merchant_data, session):
merchant = Merchant(**merchant_data)
session.add(merchant)
session.flush() # Is this needed to have Postgres generate the sequential id?
merchant.external_id = 'merchant_' + convert_to_external_id(merchant.id) # Uses the Postgres-generated id
我的问题:刷新似乎有必要获得Postgres生成的ID,但它引入了一个潜在的令人讨厌的副作用,因为它不仅刷新了新创建的商家,还刷新了会话中的其他对象。
我的问题:
谢谢!