table django_content_type doesn't exists

I have mysql database and I want to sync it’s table with my django app’s model (only one table from db should be in usage in my app).
I created model (with help of inspectdb {database-connection-name} {tablename}). Then I run

./manage.py makemigrations myappname
./manage.py migrate myappname --database=db-connection-name

But it througs ProgrammingError: table django_content_type doesn’t exists

I supposed it was raised because model of my app depends on django_content_type table (contenttypes app), so thats why I run

./manage.py migrate contenttypes --database=db-connection-name

But it alse raises ProgrammingError: table django_content_type doesn’t exists .

What should I can do?
All I need is to sync my app’s model with existing table in db.

Please, help :pray: :pray:

It’s not clear to me from your description what the relationship is between your app and this database.

There is no such thing as “syncing a table with a model”. A model is your “in python” representation of a table. They are not two separate entities.

If this is a pre-existing table in a database, you don’t need to use migrate to access it. You define your model with the db_table set to the database table and managed = False

However, aside from that, Django requires a couple tables be created in a database. If you don’t want those tables created in the same database as what your model is using, see Multiple databases. The Django database would be the default, and the database with that external table would be a secondary database.

Either way, your first “migrate” should be without any parameters, for Django to create and initialize the tables it needs to work.

yes, I have 2 databases - one is sqlite (default), second is mysql. I would like to get and work with table rows with help of my app (CRUD) and thats why I created model , and yes, I set db_table and managed = False.
before applying migrate myapp, I had applyed migrate admin, auth, sessions, contenttypes, after that all django-app tables are created in Sqlite database.
Then I “migrate” myapp with flag --database=dbconnectionname

After that those error appiers. Or django-app tables must also be in the second database? Is it possible to not create django-app tables in second db if I “migrate” and sync myapp model to second db ?

I tryed to create manually ‘django_content_type’ table in database and applyed migrate appname --database=dbconnectionname and noted that this migration creates new row in table “django_content_type” with app_label = ''admin".
I supppose “django_content_type” is required table, but I don’t know how to avoid creating row with app_label “admin” while doing migrate appname --database=dbconnectionname . Is it possible? Because in this situation another error appears "Table auth_permissions doesn’t exists ".

Let’s take a step back for a moment.

There are no requirements to run makemigrations / migrate on your secondary database(s). (Actually, there’s no requirement to use it at all - it just makes some things (a lot) easier.)

Those steps are only used if you want Django to manage the database structure.

If you’re working with pre-existing tables, or table schemas that you are managing manually, that’s not the case.

So none of this is necessary. Create the models (managed = False) and use them.
Nothing else needs to be done.

1 Like

I have the same problem
django.db.utils.ProgrammingError: (1146, “Table ‘django_nah.my_job_type_card’ doesn’t exist”)

@KenWhitesell

1 Like

If you have a question about an error message that you’d like assistance with, please open up a new topic with all the details including a description of your environment, the commands that you have run, and the complete traceback from the error message you have received.