Django - 从Sqlite3迁移到Postgresql

时间:2016-10-05 16:01:41

标签: python django postgresql heroku

我正在尝试在Heroku中部署我的Django应用程序,因此我必须切换到PostgreSQL并且我一直关注这些steps

但是当我运行python manage.py migrate

我收到以下错误:

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.

这是我的 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)

我无法弄清楚我的代码有什么问题,任何帮助都将不胜感激!

编辑:我尝试从Genre类中删除id字段,但仍然会出现同样的错误

2 个答案:

答案 0 :(得分:1)

您必须使用此命令:

python manage.py makemigrations
在命令之前

python manage.py migrate

答案 1 :(得分:0)

为什么你在Genre模型上有id字段?你不需要那个。