开发者

Django: Raw sql INSERT INTO phpbb db fails: Error #1062 Duplicate key

Hey, I'm having a signal when a django user is created I manually insert that user into a phpbb table as well. The code follows:

@receiver(post_save, sender=User)
def user_created_signal(sender, **kwargs):
    if kwargs['created']:
        user = kwargs['instance']

        fields = {
            'username': str(user.username),
            'username_clean': str(user.username),
            'user_password': "",
            'user_email': str(user.email),
            'group_id': 7,
            'user_timezone': 0,
            'user_dst': "",
            'user_lang': "sv",
            'user_type': 0,
            'user_actkey': "",
            'user_ip': "",
            'user_regdate': "",
            'user_inactive_reason': "",
            'user_inactive_time': "",
            'user_permissions': 0,
            'user_sig': "",
            'user_form_salt': "gg54jhg345",
        }
        cursor = connection.cursor()

        # Add user to phpbb

        try:

            cursor.execute("INSERT INTO spelutveckla_se.phpbb_users " + str(tuple(fields.keys())).replace("'", "") + " VALUES" + str(tuple(fields.values())))

        except Warning:
            pass

I keep get this error message: (1062, "Duplicate entry 'test_username' for key 'username_clean'")

I can see in the phpbb_user table that the user has been created. But when I remove the user and try again the same error appears. I've tried with different user names as well. It's like the code is executed more than once but I've tried to put breakpoints/raised exception to fins out but I can't find any 开发者_如何学运维sign of the code being executed more than once...

Here's the SQL:

"INSERT INTO spelutveckla_se.phpbb_users (username, user_timezone, user_form_salt, username_clean, user_dst, user_lang, user_password, user_sig, user_type, user_actkey, user_ip, user_permissions, user_regdate, group_id, user_inactive_reason, user_email, user_inactive_time) VALUES('test_username', 0, 'gg54jhg345', 'test_username', '', 'sv', '', '', 0, '', '', 0, '', 7, '', 'test@gmail.com', '')

Please help!! Thank you.


I think I had similar problem. I moved code that adds signal to another __init.py__ file. For ex. from __init__ inside yourapp, to init file in root project folder (or inversely). Maybe this will help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜