我正试图在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.
这是我的模特.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)
我不知道我的代码出了什么问题,如果有任何帮助将不胜感激!
编辑:我试图从流派类中删除id字段,但仍然得到相同的错误
最佳答案
必须使用以下命令:
python manage.py makemigrations
在你的命令之前
python manage.py migrate
关于python - Django-从Sqlite3迁移到Postgresql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39878801/