ellmetha / django-machina

A Django forum engine for building powerful community driven websites.
https://django-machina.readthedocs.org
BSD 3-Clause "New" or "Revised" License
590 stars 126 forks source link

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

Closed mtueng closed 2 years ago

mtueng commented 2 years ago

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:

mronoffon commented 2 years ago

@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 commented 2 years ago

@mronoffon I will have a look.

mronoffon commented 2 years ago

@mtueng thank you!