1.4b1 - Adding child object to parent collection in one-to-many relationship does not add it to session
See original GitHub issueThis is a change from the previous behavior in 1.3 and I am not sure if it was intentional. Reading the 1.4 docs it appears this is a bug.
Given the following entities (these are stripped down examples of my real entities):
class Parent(EntityBase):
__tablename__ = "parentTable"
__table_args__ = {"implicit_returning": False}
id = Column("ParentID", Integer, primary_key=True)
name = Column("ParentName", String(50))
children = relationship(
"Child",
cascade="all, delete-orphan",
back_populates="parent",
lazy="selectin",
)
class Child(EntityBase):
__tablename__ = "childTable"
__table_args__ = {"implicit_returning": False}
id = Column("ChildID", Integer, primary_key=True)
name = Column("ChildName", String(50))
parent_id = Column(
"ParentID", Integer, ForeignKey("parentTable.ParentID")
)
parent = relationship(
"Parent",
back_populates="children",
)
In 1.3 doing the following would automatically add the child entity to the session and performing a session.commit()
would result in the child being written to the DB. In 1.4b1 this is no longer the case when using the async version await session.commit()
. The child entity is not added to the session and does not get persisted to the database.
1.3 code - working and persists child to the DB:
parent = Parent(name="foo")
session.add(parent)
Child(name="bar", parent=parent)
session.commit() # Parent and child both written to the DB
1.4 code - does not add child to the session or write it to DB:
parent = Parent(name="foo")
session.add(parent)
Child(name="bar", parent=parent)
await session.commit() # Parent written to DB but child is not
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
OK this is because asyncio is already using SQLAlchemy future mode for its Session. If one is using the all new API in any case there’s no need to have them code to the legacy API.
cascade_backrefs is turned off for a “future” session: https://docs.sqlalchemy.org/en/14/changelog/migration_14.html#cascade-backrefs-behavior-deprecated-for-removal-in-2-0
asyncsession uses future mode, this is not documented, so we need to add a "note:: " for this: https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html#synopsis-orm
This was addressed in b5e1cb2