"Error creating new content types" with multiple DB router

75 views
Skip to first unread message

Carlton Gibson

unread,
Dec 15, 2015, 4:30:25 PM12/15/15
to Django users
Hi, 

Using multiple DBs, with most models in the "default" db but models for a single app being stored in a second DB, I've more or less copied the example from the docs for a custom database router. (My example is essentially identical to the `AuthRouter` class given.)

This works perfectly except when I run the migration on the second DB. 

The app relations are all created but I then get an error creating the ContentType...

RuntimeError: Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually.

This occurs because the relation "django_content_type" does not exist in the second database — that's correct: I'm expecting content types to be written only to the "default" db. 

My question is why is this error occurring? 

1. As per the docs, in my router, I am returning "None if there is no suggestion" for all the router methods — particularly `db_for_write`, which seems relevant here. 
2. Looking at the source, the default router returns `DEFAULT_DB_ALIAS` if all the custom routers return None. 

So 3. I can't see the error. :-) — I'm expecting the content type to be written to the default db. 

I can work around the issue here — but I'd like to understand it. Can anyone with experience of this explain what I've done wrong?

Kind Regards,

Carlton 


Jim T

unread,
Apr 12, 2016, 8:50:39 AM4/12/16
to Django users
Carlton, we have the exact same issue you described.  It took a lot of searching to even find someone who has the same problem which led us to the conclusion that we just didn't understand how to perform migrations with DB Routers in play.  As you described, we have:

Database A - contains the schema for auth, sessions, content-type, models for app A
Database B - contains the models for app B

Migrations for app A function correctly.
Migrations for app B do not function correctly.

Our goal is the same as yours - to have models for app B (in Database B) store it's content-type in Database A, which is actually the point of having an "app" label in the content types table.  We follow the same suggestions and examples and yield the same error messages.

We are convinced the solution is some subtlety in the DB Router code that we missed and continue to investigate.  Did you ever find the solution to this?

Thanks
-Jim
Reply all
Reply to author
Forward
0 new messages