2

I'm trying to deploy my Django app in Heroku, so I have to switch to PostgreSQL and I've been following these steps

However when I run python manage.py migrate

I get the following error:

C:\Users\admin\trailers>python manage.py migrate
Operations to perform:
  Apply all migrations: auth, movies, sessions, admin, contenttypes
Running migrations:
  Rendering model states... DONE
  Applying movies.0012_auto_20160915_1904...Traceback (most recent call last):
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\backends\
utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: foreign key constraint "movies_movie_genre_genre_id_d
9d93fd9_fk_movies_genre_id" cannot be implemented
DETAIL:  Key columns "genre_id" and "id" are of incompatible types: integer and
character varying.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\managem
ent\__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\managem
ent\__init__.py", line 342, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\managem
ent\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\managem
ent\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\managem
ent\commands\migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\migration
s\executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_ini
tial)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\migration
s\executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_
initial)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\migration
s\executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\migration
s\migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\migration
s\operations\fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\backends\
base\schema.py", line 482, in alter_field
    old_db_params, new_db_params, strict)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\backends\
base\schema.py", line 634, in _alter_field
    params,
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\backends\
base\schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\backends\
utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\backends\
utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\utils.py"
, line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\utils\six.py
", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\backends\
utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: foreign key constraint "movies_movie_genre_gen
re_id_d9d93fd9_fk_movies_genre_id" cannot be implemented
DETAIL:  Key columns "genre_id" and "id" are of incompatible types: integer and
character varying.

Here is my models.py

class Genre(models.Model):
    id = models.IntegerField(primary_key=True)
    genre = models.CharField(max_length=255)

class Person(models.Model):
    name = models.CharField(max_length=128)

class Movie(models.Model):
    title = models.CharField(max_length=511)
    tmdb_id = models.IntegerField(null=True, blank=True, unique=True)
    release = models.DateField(null=True, blank=True)
    poster = models.TextField(max_length=500, null=True)
    backdrop = models.TextField(max_length=500, null=True, blank=True)
    popularity = models.TextField(null=True, blank=True)
    runtime = models.IntegerField(null=True, blank=True)
    description = models.TextField(null=True, blank=True)
    director = models.ManyToManyField(Person, related_name="directed_movies")
    actors = models.ManyToManyField(Person, related_name="acted_movies")
    genre = models.ManyToManyField(Genre)

class Trailer(models.Model):
    movie = models.ForeignKey(Movie, on_delete=models.CASCADE, null=True)
    link = models.CharField(max_length=100)

I can't figure out what's wrong with my code, any help would be appreciated!

Edit: I tried removing the id field from the Genre class and I still get the same error

Community
  • 1
  • 1
mari
  • 325
  • 1
  • 5
  • 16

1 Answers1

1

You must use this command:

python manage.py makemigrations

before your command

python manage.py migrate
Piu
  • 135
  • 2
  • 20
  • "Key columns "genre_id" and "id" are of incompatible types: integer and character varying." Your genre_id is defined as Integer field and Movie.genre is defined as a field accept varying character. Many to many cannot accept different type of foreign key. So, you can remove id field in model class Genre, it is not necessary, because Django have created the id field for you by default. – Piu Oct 05 '16 at 16:34
  • but what if I want to assign the ids manually rather than have Django assign them by default? – mari Oct 05 '16 at 16:48
  • If you want to keep your id field, you can see about https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships Membership, this may help. – Piu Oct 05 '16 at 17:00