Django Programming error column does not exist even after running migrations

前端 未结 9 1020
南旧
南旧 2020-12-08 20:54

I run python manage.py makemigrations and I get: No changes detected Then, python manage.py migrate and I get: No migrations to apply.

相关标签:
9条回答
  • 2020-12-08 21:04

    The issue was in the Models for me, for some reason Django was adding '_id' to the end of my Foreign Key column. I had to explicitly set the related named to the Foreign Key. Here 'Cards' is the parent table and 'Prices' is the child table.

    class Cards(models.Model):
        unique_id = models.CharField(primary_key=True, max_length=45)
        name = models.CharField(max_length=225)   
    
    class Prices(models.Model):
            unique_id = models.ForeignKey(Cards, models.DO_NOTHING)
    

    Works after changing to:

    class Cards(models.Model):
        unique_id = models.CharField(primary_key=True, max_length=45)
        name = models.CharField(max_length=225)   
    
    class Prices(models.Model):
            unique_id = models.ForeignKey(Cards, models.DO_NOTHING, db_column='unique_id')
    
    0 讨论(0)
  • 2020-12-08 21:05

    I got the same problem (column not exist) but when I try to run migrate not with makemigrations (it is the same issue I believe)

    • Cause: I removed the migration files and replaced them with single pretending intial migration file 0001 before running the migration for the last change

    • Solution:

      1. Drop tables involved in that migration of that app (consider a backup workaround if any)
      2. Delete the rows responsible of the migration of that app from the table django_migrations in which migrations are recorded, This is how Django knows which migrations have been applied and which still need to be applied.

    And here is how solve this problem:

    • log in as postgres user (my user is called posgres):

      sudo -i -u postgres

    • Open an sql terminal and connect to your database:

      psql -d database_name

    • List your table and spot the tables related to that app:

      \dt

    • Drop them (consider drop order with relations):

      DROP TABLE tablename ;

    • List migration record, you will see migrations applied classified like so:

    id | app | name | applied
    --+------+--------+---------+

    SELECT * FROM django_migrations;
    
    • Delete rows of migrations of that app (you can delete by id or by app, with app don't forget 'quotes'):

      DELETE FROM django_migrations WHERE app='yourapp';

    • log out and run your migrations merely (maybe run makemigrations in your case):

      python manage.py migrate --settings=your.settings.module_if_any

    Note: it is possible that in your case will not have to drop all the tables of that app and not all the migrations, just the ones of the models causing the problem.

    I wish this can help.

    0 讨论(0)
  • 2020-12-08 21:11

    Here's what i tried and it worked:

    • Go and add manually the column to your table
    • run python manage.py makemigrations
    • go back drop that column you added
    • run python manage.py migrate
    0 讨论(0)
  • 2020-12-08 21:13

    Just remove corresponding row migrations for that model in 'django_migrations' model in database.

    And re run python manage.py migrate app_name

    0 讨论(0)
  • 2020-12-08 21:15

    I had a similar issue - the error message appeared when I clicked on the model on the django-admin site. I solved it by commenting out the field in models.py, then running migrations. Following this I uncommented the field and re ran the migrations. After that the error message disappeared.

    0 讨论(0)
  • 2020-12-08 21:19

    My case might be a bit obscure, but if it helps someone, it is worth documenting here.

    I was calling a function in one of my migrations, which imported a Model of said migration regularly, i.e.

    from myApp.models import ModelX
    

    The only way models should be imported in migrations would be using e.g. RunPython:

    def myFunc(apps, schema_editor): 
        MyModel = apps.get_model('myApp 'MyModel')
    

    and then calling that function like so:

    class Migration(migrations.Migration):
        operations = [
            migrations.RunPython(initialize_mhs, reverse_code=migrations.RunPython.noop),
        ]
    

    Additionally the original import worked until I modified the model in a later migration, making this error harder to locate.

    0 讨论(0)
提交回复
热议问题