django.db.utils.IntegrityError:(1062,“键'content_type_id'的重复条目'22 -add_'”) [英] django.db.utils.IntegrityError: (1062, "Duplicate entry '22-add_' for key 'content_type_id'")

查看:117
本文介绍了django.db.utils.IntegrityError:(1062,“键'content_type_id'的重复条目'22 -add_'”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django多数据库路由器概念,其中有多个具有不同数据库的站点。基本数据库用户将使用所有其他子站点登录。

I am using django multiple DB router concepts, having multiple sites with different db's. Base database user will login with all other sub sites.

当我在基础站点中尝试 syncdb 时,它可以正常工作(随时),但与其他人一起尝试 syncdb 网站仅在第一次工作,如果我们下次再尝试,它将引发以下类似错误

When i try syncdb in base site its worked properly(at any time), but trying syncdb with other sites works first time only, if we try next time on-wards it throws integiry error like below


  • django.db.utils .IntegrityError:(1062,键'content_type_id'的重复条目
    '22 -add_somesame')

  • django.db.utils.IntegrityError: (1062, "Duplicate entry '22-add_somesame' for key 'content_type_id'")

一旦我在该项目中删除了多个数据库路由器设置,就意味着syncdb可以正常工作(随时)。

Once i removed multiple DB router settings in that project means syncdb works properly(at any time).

那么这与多个数据库路由器有关吗?还是什么?

So is this relates to multiple db router? or what else?

请有人对此提出建议,谢谢。

Please anyone advise on this, thanks.

推荐答案

这里的问题是db路由器和django系统对象。对于多个数据库和路由器,我也遇到过相同的问题。我记得这里的问题是auth.permission内容类型,它们在数据库之间混杂在一起。 syncdb脚本会尝试在所有数据库中创建这些对象,然后b为某些对象创建权限内容类型,该对象的ID已为本地模型保留。

The problem here is with the db router and django system objects. I've experienced the same issue with multiple DBs and routers. As I remember the problem here is with the auth.permission content types, which get mixed in between databases. The syncdb script otherwise tries to create these in all databases, and theb it creates permission content type for some object, which id is already reserved for a local model.

以下

BASE_DB_TYPES = (
 'auth.user',
 'auth.group',
 'auth.permission',
 'sessions.session',

,然后在db路由器中:

and then in the db router:

def db_for_read(self, model, **hints):
    if hasattr(model, '_meta') and str(model._meta) in BASE_DB_TYPES:
        return 'base_db' # the alias of base db that will store users
    return None # the default database, or some custom mapping

编辑

此外,该异常可能表明您声明对模型'< add_somesame '的许可code> somesame ,而Django会自动创建 add _ delete _ ,所有对象的 edit _ 权限。

Also, the exception might say that you're declaring a permission 'add_somesame' for your model 'somesame', while Django automatically creates add_, delete_, edit_ permissions for all objects.

这篇关于django.db.utils.IntegrityError:(1062,“键'content_type_id'的重复条目'22 -add_'”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆