python - Django - 从 Sqlite3 迁移到 Postgresql

标签 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 字段,但我仍然遇到相同的错误

最佳答案

你必须使用这个命令:

python manage.py makemigrations

在你的命令之前

python manage.py migrate

关于python - Django - 从 Sqlite3 迁移到 Postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39878801/

相关文章:

python - 导入错误 : cannot import name 'trace' from 'tensorflow.python.profiler'

python - 无法通过模型实例访问管理器

php - IBM Bluemix PostgreSQL 和 psql

python - 如何计算分组 df 的差异?

python - 有没有办法在 python 中将 xml 子标签存储在列表中?

python - 在django中选择过滤后的相关对象

SQL:从 "nothing"中选择一个数字列表

ruby - rails postgreSQL 无法使此查询正常工作(SUM、Group Order)

python - 通过使用标题作为列名加载 csv 文件来创建数据库(并添加以文件名作为名称的列)

django 1.3 templatestag 如果字符串包含字符串列表