question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

1.4b1 - Adding child object to parent collection in one-to-many relationship does not add it to session

See original GitHub issue

This 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:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
zzzeekcommented, Jan 12, 2021

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

In 2.0, the default behavior will be that “cascade_backrefs” is False, and additionally there will be no “True” behavior as this is not generally a desirable behavior. When 2.0 deprecation warnings are enabled, a warning will be emitted when a “backref cascade” actually takes place. To get the new behavior, either set relationship.cascade_backrefs to False on the target relationship, as is already supported in 1.3 and earlier, or alternatively make use of the Session.future flag to 2.0-style mode:

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

0reactions
jvanascocommented, Sep 18, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found