मैंने अभी एक ही समस्या में भाग लिया है, और परीक्षण के बाद मुझे पता चला है कि इनमें से कोई भी जवाब पर्याप्त नहीं है।
वर्तमान में, या SQLAlchemy .6+ के रूप में, वहाँ एक बहुत ही सरल समाधान (अगर यह पहले संस्करण में उपलब्ध है मैं हालांकि मैं सोच भी यह होता है पता नहीं है,) है:
session.refresh()
f = Foo(bar=x)
session.add(f)
session.flush()
# At this point, the object f has been pushed to the DB,
# and has been automatically assigned a unique primary key id
f.id
# is None
session.refresh(f)
# refresh updates given object in the session with its state in the DB
# (and can also only refresh certain attributes - search for documentation)
f.id
# is the automatically assigned primary key ID given in the database.
इसे कैसे करना है कि:
तो, अपने कोड कुछ इस तरह दिखेगा।
क्या आप एसए इंजन को 'echo = True' के साथ प्रारंभ कर सकते हैं, और देखें कि फ्लैश-टाइम पर SQL को निष्पादित किया जाता है? आप जो वर्णन करते हैं * * काम करना चाहिए और आपको आईडी देना चाहिए, लेकिन हो सकता है कि कोई अन्य समस्या हो जिसके परिणामस्वरूप f.id कोई नहीं हो। –