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

Occasionally run into TimelineEvent.MultipleObjectsReturned get() returned more than one #717

Open
alphatownsman opened this issue Jul 8, 2024 · 0 comments

Comments

@alphatownsman
Copy link
Contributor

alphatownsman commented Jul 8, 2024

from

    @classmethod
    def add_post(cls, identity, post):
        """
        Adds a post to the timeline if it's not there already
        """
        event, created = cls.objects.get_or_create(
            identity=identity,
            type=cls.Types.post,
            subject_post=post,
            subject_identity=post.author,
            defaults={"published": post.published or post.created},

It's hard to avoid race condition I think, so it's better to just add a unique constraint

DELETE FROM activities_timelineevent a
USING activities_timelineevent b 
WHERE a.ctid < b.ctid
AND a.identity_id=b.identity_id
AND a.type=b.type
AND a.type='post'
AND a.subject_post_id=b.subject_post_id
AND a.subject_identity_id=b.subject_identity_id;


CREATE UNIQUE INDEX idx_uniq_timelineevent_post ON activities_timelineevent(identity_id, subject_post_id, subject_identity_id) WHERE type='post';

it can be added to model, but would like to see if anyone has other suggestions before I create a PR.

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