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

UniqueViolation when loading data (loaddata with a fixture) #262

Closed
mtueng opened this issue Jan 19, 2022 · 3 comments · Fixed by #263
Closed

UniqueViolation when loading data (loaddata with a fixture) #262

mtueng opened this issue Jan 19, 2022 · 3 comments · Fixed by #263

Comments

@mtueng
Copy link
Contributor

mtueng commented Jan 19, 2022

I noticed this bug during a database migration from mariadb to postgresql where I used dumpdata/loaddata.
The loaddata failed with this error:

<class 'psycopg2.errors.UniqueViolation'>: duplicate key value violates unique constraint "forum_member_forumprofile_user_id_key" DETAIL: Key (user_id)=(1743) already exists.

I found out that this is the SQL query that triggered the error:

INSERT INTO "forum_member_forumprofile" ("id", "user_id", "avatar", "signature", "posts_count", "_signature_rendered") VALUES (%s, %s, %s, %s, %s, %s)" % (1, 1743, '', '***', 1, '***')

The root cause is that a pre_save hook in machina/apps/forum_member/receivers.py already does a get_or_create, which triggered this SQL query to be executed before the one above:

INSERT INTO "forum_member_forumprofile" ("user_id", "avatar", "signature", "posts_count", "_signature_rendered") VALUES (%s, %s, %s, %s, %s) RETURNING "forum_member_forumprofile"."id" % (1743, '', None, 0, None)

Long story short: This can be fixed by disabling the pre_save hooks when loading data (i.e. when kwargs['raw'] == True). I will create a pull request.

Using:

  • Django 3.2.9
  • django-machina 1.1.5
  • postgresql 11+200+deb10u4
  • python3.7 3.7.3-2+deb10u3
  • Debian GNU/Linux 10 (buster)
@mronoffon
Copy link

mronoffon commented Jan 19, 2022

@mtueng

hi,

After following the installation instructions provided, and figuring out my first error (#261), i loaded the example fixtures and received this error msg:

Reverse for 'forum' with arguments '('a-simple-sub-forum-1', Hashid(3): qz0m9d5wd3rx1)' not found. 1 pattern(s) tried: ['forum/forum/(?P[^/]+)\-(?P[0-9]+)/\Z']

In fact, without more detailed documentation, i've been getting errors since the installation. One thing that threw me was the fact that i had to be logged in just to see the index page (issue #261). Is there some way of resolving the 'reverse' error and is there an 'anonymous user' setting for the default page?

thanks in advance

@mtueng
Copy link
Contributor Author

mtueng commented Jan 19, 2022

@mronoffon
I will have a look.

@mronoffon
Copy link

@mtueng
thank you!

ellmetha added a commit that referenced this issue Jan 30, 2022
Fix #262 - Disable pre_save receivers during loaddata of fixtures
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

Successfully merging a pull request may close this issue.

2 participants