django.db.utils.IntegrityError: (1062, “Duplicate entry '22-add_' for key 'content_type_id'”)

强颜欢笑 提交于 2019-12-08 01:25:27
Tisho

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.

I have the following

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

)

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!