Django.db.utils.IntegrityError: (1062, "Duplicate entry" 22 -add_ "for key" content_type_id ")

I use django several DB router concepts, having multiple sites with different db. The user of the base database will log in with all the other subsites.

When I try to syncdb on the base site to work correctly (anytime), but trying syncdb with other sites only works the first time, if we try the next time, it throws an error like integery, as shown below

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

As soon as I delete several parameters of the database router in this project, it means that syncdb is working correctly (at any time).

So is this related to multiple db routers? or what else?

Please someone advise on this, thanks.

+1
source share
1 answer

The problem here is with the db router and django system objects. I ran into the same problem with multiple databases and routers. As far as I remember, the problem here is related to auth.permission content types that mix between databases. Syncdb script otherwise tries to create them in all databases, and theb creates a permission content type for some object that is already reserved for the 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

In addition, an exception might say that you are declaring add_somesame permission for your somesame model, and Django will automatically create add_ , delete_ , edit_ for all objects.

+1
source

All Articles