farahats9 / sqlalchemy-celery-beat

Celery Periodic Tasks backed by the SQLAlchemy
MIT License
43 stars 6 forks source link

fixed bug to return only related table data in join #1

Closed mbronstein1 closed 9 months ago

mbronstein1 commented 9 months ago

Feature re: Select Periodic Task joined with discriminator table

Expected Behavior

When querying on PeriodicTask table using session.query(PeriodicTask), the joined schedule table is related to the schedule_id AND the discriminator.

Actual Behavior

When querying, if the same id exists in both schedule tables, both joined tables are returned.

Test sample

    def test_PeriodicTask_specifyjoin(self):
        p = self.create_model_interval(self.session, schedule(timedelta(seconds=3)))
        c = self.create_model_crontab(self.session, crontab(minute="3", hour="3"))

        self.session.add(p)
        self.session.add(c)
        self.session.commit()

        p = self.session.query(PeriodicTask).first()

        assert p.schedule_id == 1
        assert c.schedule_id == 1

        assert p.discriminator == 'intervalschedule'
        assert p.model_crontabschedule is None # this would fail prior to these changes
        assert p.model_intervalschedule is not None

        assert c.discriminator == 'crontabschedule'
        assert c.model_intervalschedule is None # this would fail as well
        assert c.model_crontabschedule is not None 

Thoughts

In the older implementation (celery_sqlalchemy_scheduler), there were two separate foreign keys specifically connected by crontab_id and interval_id. The newer version uses the discriminator column and the reasoning behind this seems vague.

Collaborators

Matthew Bronstein Bazyl Horsey

farahats9 commented 9 months ago

Great work and thanks for contributing.