Django:原始 sql INSERT INTO phpbb db 失败:错误 #1062 重复密钥
嘿, 当创建 django 用户时,我收到一个信号,我也将该用户手动插入到 phpbb 表中。代码如下:
@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
我不断收到此错误消息: (1062,“键‘username_clean’的重复条目‘test_username’”)
我可以在 phpbb_user 表中看到该用户已创建。但是当我删除用户并重试时,会出现相同的错误。我也尝试过使用不同的用户名。这就像代码被执行了不止一次,但我试图将断点/引发的异常添加到 fins 中,但我找不到任何代码被执行多次的迹象......
这是 SQL:
“插入 spelutveckla_se.phpbb_users(用户名, 用户时区、用户表单盐、 用户名_clean、用户_dst、用户_lang、 用户密码、用户签名、用户类型、 用户_actkey、用户_ip、 用户权限、用户注册日期、 组 ID、用户非活动原因、 用户电子邮件、用户非活动时间) 值('测试用户名', 0, 'gg54jhg345', '测试用户名', '', 'sv', '', '', 0, '', '', 0, '', 7, '', '[电子邮件受保护]', '')
请帮忙!谢谢。
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, '',
'[email protected]', '')
Please help!! Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我也有类似的问题。我将添加信号的代码移至另一个 __init.py__ 文件。对于前。从应用程序内的 __init__ 到根项目文件夹中的 init 文件(或相反)。也许这会有所帮助。
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.