Fail - Unknown column name django_content_type

Hi everyone

Im having some trouble with django migrations and had to redo the migrations while trying to keep data in the tables in Mysql. It seems like i have somewhat succeeded but we are getting this constant errors on content type. BAsically what ive done is as follows roughly

  • ensure database and models are the same
  • delete all migrations.py files
  • fake migrate after makemigrations
  • make changes to models and run makemigrate and migrate

It creates those new tables but im worried about these eorrors that i could have broken something else. If you have any resource that explain these errors or whats happening i would check further. If you have any hint please do let me know

Yes, you probably have broken something.

At this point, my suggestion is to save your data, recreate the database, and reload your data.

You could manually inspect and correct everything in your database, but that’s going to be a significant manual effort with a high probability of missing something along the way or of making a mistake.

In the future, you do want to take the position that you do not want to arbitrarily delete migration files without being aware of all the potential implications of doing so.

ok thanks Ken. But is there any resource that would help explain somewhat what content type does. Also this problem would it be an issue with postgress as well , i seem to be hitting frequent issues with mysql after the first mess up (overwriting migration files from github into production)

Yes, The contenttypes framework docs cover what ContentType does, how it’s structured, and how it can be used.

The problem isn’t with MySQL or PostgreSQL - it’s an issue with your current database relative to how migration files have been handled. Your best solution at this point is to save your data tables, drop and recreate the database, and then reload your data - assuming you don’t have any foreign keys referencing Django system tables.

Just to close the loop on this, the solution as you say and easiest was to recreate tables. The quickest and safest way for me

  1. Delete all migration files in apps
  2. Change schema name in settings.py
  3. Makemigrations and migrate
  4. importing was a problem as table structures had changed from previous, so SQL inserts had to be precise and took a while. Not sure if there is a better way around it

one table example SQL insert as below ; and many others but just to be careful with the inserts and constraints as table structure has changed since you are making it from fresh

insert into trackemupgrade.UploadExcel_historicalactionitems
(id,StudyActionNo,StudyName,Facility,ProjectPhase,Cause,Safeguard,Consequence,Recommendations,InitialRisk,ResidualRisk,Response,Organisation,
Disipline,Subdisipline,FutureAction,DueDate,QueSeries,QueSeriesTarget,Guidewords,Deviation,Revision,DateCreated,history_id,history_date,
history_change_reason,history_type,history_user_id)
select
trackem.UploadExcel_historicalactionitems.id, trackem.UploadExcel_historicalactionitems.StudyActionNo, trackem.UploadExcel_historicalactionitems.StudyName, trackem.UploadExcel_historicalactionitems.Facility, trackem.UploadExcel_historicalactionitems.ProjectPhase, trackem.UploadExcel_historicalactionitems.Cause, trackem.UploadExcel_historicalactionitems.Safeguard, trackem.UploadExcel_historicalactionitems.Consequence, trackem.UploadExcel_historicalactionitems.Recommendations, trackem.UploadExcel_historicalactionitems.InitialRisk, trackem.UploadExcel_historicalactionitems.ResidualRisk, trackem.UploadExcel_historicalactionitems.Response, trackem.UploadExcel_historicalactionitems.Organisation, trackem.UploadExcel_historicalactionitems.Disipline, trackem.UploadExcel_historicalactionitems.Subdisipline, trackem.UploadExcel_historicalactionitems.FutureAction, trackem.UploadExcel_historicalactionitems.DueDate, trackem.UploadExcel_historicalactionitems.QueSeries, trackem.UploadExcel_historicalactionitems.QueSeriesTarget, trackem.UploadExcel_historicalactionitems.Guidewords, trackem.UploadExcel_historicalactionitems.Deviation, trackem.UploadExcel_historicalactionitems.Revision, trackem.UploadExcel_historicalactionitems.DateCreated, trackem.UploadExcel_historicalactionitems.history_id, trackem.UploadExcel_historicalactionitems.history_date, trackem.UploadExcel_historicalactionitems.history_change_reason, trackem.UploadExcel_historicalactionitems.history_type, trackem.UploadExcel_historicalactionitems.history_user_id
from trackem.UploadExcel_historicalactionitems;