Django column “name” of relation “django_content_type” does not exist

后端 未结 8 1810
忘了有多久
忘了有多久 2020-12-23 14:31

I keep getting the following error when doing a migration (python manage.py migrate):

django.db.utils.ProgrammingError: column \"name\" of relation \"django_         


        
相关标签:
8条回答
  • 2020-12-23 14:45

    I know that is an old question, but this might help some one. I was getting this error too. The problem was that content_types had a migration called 0002_remove_content_type_name that remove the column "name".
    After remove migration data from table and folder this steps works for me:

    ./manage.py makemigrations myappname
    ./manage.py migrate myappname
    ./manage.py migrate --fake contenttypes
    

    If you have sure that the rest of your db reflect your migration, you can use:

    ./manage.py migrate --fake
    

    See the result with:

    ./manage.py showmigrations
    

    After that, all your migrations should be marked and you should be fine

    0 讨论(0)
  • 2020-12-23 14:45

    Some part of you code is trying to access the django_content_type table when it dose not exist, leading to a ProgrammingError. A work round would be to wrap that part in a try except. Sample

    try:
        seller_content_type = ContentType.objects.get_for_model(Seller)
        add_seller_permission = Permission.objects.get(
            codename='add_seller',
            content_type=seller_content_type,
        )
    except ProgrammingError:
        add_seller_permission = None
    

    You can actually replace add_seller_permission = None with just pass, but the above helps if you are using the value somewhere else, so it remains available at least till the migration is done.

    0 讨论(0)
  • 2020-12-23 14:52

    Encountered this when upgrading to 1.8 and migrating from MySQL to Postgres.

    I can't explain why the error occurs, but I was able to get around it by manually adding the column:

    1. Delete all migrations

    2. Delete records from django_migrations

    3. Manually add name column:

      ALTER TABLE django_content_type ADD COLUMN name character varying(50) NOT NULL DEFAULT 'someName';
      
    4. Run fake initial: $ python manage.py migrate --fake-initial

    Edit 12/2016: I'm recommending this as a workaround, more suited for personal projects or local environments and not production environments. Obviously if you care about your migration history this is not the way to go.

    0 讨论(0)
  • 2020-12-23 14:54

    Another variant that worked for me (from a blank database) was:

    ./manage.py makemigrations myappname
    ./manage.py migrate
    

    A variant which DID NOT work was:

    ./manage.py makemigrations myappname
    ./manage.py migrate myappname
    

    Or, rather, the sequence would apparently work the first time, but would not work the second time, complaining then that django_content_type did not exist.

    The working (first 2-line) variant above seems to be idempotent.

    (edited: to remove redundant second line from original 3-line working version)

    0 讨论(0)
  • 2020-12-23 14:56

    I was getting this error after we decided to remove migrations from version control (git) and create a new database from scratch.

    Something that worked for me (although to be honest I'm not sure why) was to run 'makemigrations' for each app. so, 'python manage.py makemigrations app1', 'python manage.py makemigrations app2', and so on for each Django app.

    Finally, I just ran 'python manage.py migrate', crossed my fingers, and it worked.

    Any insight into how/why this worked would be helpful.

    0 讨论(0)
  • 2020-12-23 14:56

    For me, I ran makemigrations for each of my INSTALLED_APPS with this command:

    python manage.py makemigrations compressor
    

    change compressor with your INSTALLED_APPS. then successfully migrated with this command:

    python manage.py migrate
    
    0 讨论(0)
提交回复
热议问题