1

well, I am implementing a multiple db feature on an exist django project. After I create and setup the secondary db to django, the first thing I did is running syncdb and migrate, which will create a lot built-in django tables, includes django_content_type table.

Then I realize, the records in the new django_content_type table are different from the one in primary db. To be more specific, the primary key is different.

Let's say I have a record in primary db django_content_type table looks like following:

 id | name | app_label | model 
----+------+-----------+-------
 33 | Tag  | taggit    | tag

However, after syncdb and running migrations, I got following in my secondary db:

 id | name | app_label | model 
----+------+-----------+-------
 11 | Tag  | entities  | tag

I kind of hate to have two inconsistent django_content_type sitting in my two db. I tried to truncate one of them and load a fixture from another and failed, since django_content_type table is referred by most of other tables.

What is best way to handle things like this? I try to convince myself this won't matter, but I am afraid inconsistency will bite me in the future, so I really want to solve this issue.

Thanks in advance.

1 Answer 1

3

It's a common issue since you are using different databases. The main concern here is when some models are using ContentType as a field which will be translated to an FK like content_type_object_id in the database, and when those are different in the databases might cause an error. Therefore, because you need two databases in your application you should manager your calls using db_manager() not using()

For example:

In your application, this might cause an error

ContentType.objects.get_for_model(Tag)

Because you are using a manager method get_for_model() and you need to specify the database before.

ContentType.objects.db_manager(...).get_for_model(Tag)

In this way you will make sure that you are getting the ContentType based on the PK that has been addressed in the specified database.


I am not saying that there is no solution, there might be a solution to sync the two databases. However, I wrote this to not worry you about the situation.

I really prefer that they would be consistent, but multiple databases never do to me.

Sign up to request clarification or add additional context in comments.

1 Comment

Seems a reasonable answer, though I really want to get those pk consistent cross databases. I will dig deeper and see if there is a solution. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.