Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue updating aggregate when setting foreign key to Null #735

Open
RaHus opened this issue Apr 1, 2024 · 1 comment
Open

Issue updating aggregate when setting foreign key to Null #735

RaHus opened this issue Apr 1, 2024 · 1 comment

Comments

@RaHus
Copy link

RaHus commented Apr 1, 2024

I have 2 tables defined as such

class BaseUserList(Base):
    id = Column(Integer, primary_key=True)
    ...
    event = relationship("Event", back_populates="base_user_list")

    @aggregated(
        "event", Column(Boolean, server_default=text("false"), nullable=False)
    )
    def _has_event(self):
        return func.count(Event.id) > 0

class Event(Base):
    id = Column(Integer, primary_key=True)
    ...
    base_user_list_id = Column(
        Integer, ForeignKey("user_list.id", ondelete="SET NULL"), index=True
    )
    base_user_list = relationship("BaseUserList", back_populates="event")

When i attach a list to the event the aggregate updates correctly

 ev=Event.q.get(83) # q is DBSession.query(Event)
 ev.base_user_list_id=2
 ev.flush()
 
 INFO  [sqlalchemy.engine.Engine][MainThread] UPDATE event SET updated_at=%(updated_at)s, base_user_list_id=%(base_user_list_id)s WHERE event.id = %(event_id)s
 INFO  [sqlalchemy.engine.Engine][MainThread] [cached since 2127s ago] {'updated_at': datetime.datetime(2024, 4, 1, 9, 20, 50, 634580), 'base_user_list_id': 2, 'event_id': 83}
 INFO  [sqlalchemy.engine.Engine][MainThread]  UPDATE user_list SET updated_at=%(updated_at)s, _has_event=(SELECT count(event.id) > %(count_1)s AS anon_1 
FROM event 
WHERE user_list.id = event.base_user_list_id) WHERE user_list.id IN (%(id_1_1)s)
 INFO  [sqlalchemy.engine.Engine][MainThread] [generated in 0.00018s] {'updated_at': datetime.datetime(2024, 4, 1, 9, 20, 50, 636710), 'id_1_1': 2}

But when i clear the foreign key in the Event class, the update query is wrong

ev.base_user_list_id=None

ev.flush()

UPDATE event SET updated_at=%(updated_at)s, base_user_list_id=%(base_user_list_id)s WHERE event.id = %(event_id)s
INFO  [sqlalchemy.engine.Engine][MainThread] [cached since 233.5s ago] {'updated_at': datetime.datetime(2024, 4, 1, 9, 26, 43, 931450), 'base_user_list_id': None, 'event_id': 83}
 INFO  [sqlalchemy.engine.Engine][MainThread] UPDATE user_list SET updated_at=%(updated_at)s, _has_event=(SELECT count(event.id) > %(count_1)s AS anon_1 
FROM event 
WHERE user_list.id = event.base_user_list_id) WHERE user_list.id IN (NULL)
INFO  [sqlalchemy.engine.Engine][MainThread] [generated in 0.00067s] {'updated_at': datetime.datetime(2024, 4, 1, 9, 26, 43, 938208), 'count_1': 0}

As you can see in the update query issued by sqlalchey_utils:

UPDATE user_list SET updated_at=..., _has_event=(SELECT count(event.id) > 0 AS anon_1 
FROM event 
WHERE user_list.id = event.base_user_list_id) WHERE user_list.id IN (NULL)

The update condition is WHERE user_list.id IN (NULL) which is something that never makes sense

Here the query generated should have used the value for base_user_list_id before the update

Also tried adding
base_user_list = relationship("BaseUserList", back_populates="event", uselist=False)
but it still results in the exact same query

@RaHus
Copy link
Author

RaHus commented Apr 22, 2024

@kvesteri Any feedback for this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant