因此,我有一个要部署到Heroku的应用程序。我使用django-heroku,因此它可以自动运行postgres-sql。
我将release: python mannge.py migrate
添加到了Procfile
中。
构建完成后,我收到一个部署失败错误,提示为Release command failed
。打开日志时,出现此错误:
psycopg2.ProgrammingError: cannot cast type timestamp with time zone to integer
LINE 1: ...LUMN "established" TYPE integer USING "established"::integer
^
我的带有established
字段的模型是这样的:
from django.db import models
# Create your models here.
class FederalMinistry(models.Model):
full_name = models.CharField(max_length=400)
short_name = models.CharField(max_length=20)
description = models.TextField()
established = models.PositiveIntegerField()
# logo = models.ImageField(upload_to='fed_min_logos', blank=True, null=True, default='N/A')
current_minister = models.CharField(max_length=500)
permanent_secretary = models.CharField(max_length=500, default='N\A')
headquarters = models.CharField(max_length=100, default='Abuja')
twitter = models.URLField(blank=True, null=True, default='N/A')
website = models.URLField(blank=True, null=True, default='N/A')
class Meta:
verbose_name_plural = 'Federal Ministries'
def __str__(self):
return self.full_name
请为什么我得到该错误及其修复方法。我不懂postgres。
这是要求的完整追溯:
/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
Operations to perform:
Apply all migrations: admin, airports, auth, contenttypes, federal_ministries, sessions, states, universities
Running migrations:
Applying federal_ministries.0002_auto_20190310_0129...Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: cannot cast type timestamp with time zone to integer
LINE 1: ...LUMN "established" TYPE integer USING "established"::integer
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 203, in handle
fake_initial=fake_initial,
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 523, in alter_field
old_db_params, new_db_params, strict)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/schema.py", line 122, in _alter_field
new_db_params, strict,
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 663, in _alter_field
params,
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 133, in execute
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
Q File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: cannot cast type timestamp with time zone to integer
LINE 1: ...LUMN "established" TYPE integer USING "established"::integer
这是federal_ministries.0002_auto_20190310_0129
迁移文件:
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('federal_ministries', '0001_initial'),
]
operations = [
migrations.AlterModelOptions(
name='federalministry',
options={'verbose_name_plural': 'Federal Ministries'},
),
migrations.AlterField(
model_name='federalministry',
name='established',
field=models.PositiveIntegerField(),
),
]
以及依赖项0001_initial
文件:
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='FederalMinistry',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('full_name', models.CharField(max_length=400)),
('short_name', models.CharField(max_length=20)),
('description', models.TextField()),
('established', models.DateTimeField()),
('current_minister', models.CharField(max_length=500)),
('permanent_secretary', models.CharField(default='N\\A', max_length=500)),
('headquarters', models.CharField(default='Abuja', max_length=100)),
('twitter', models.URLField(blank=True, default='N/A', null=True)),
('website', models.URLField(blank=True, default='N/A', null=True)),
],
),
]
答案 0 :(得分:0)
如果要在PostgreSQL中将timestamp
转换为PositiveIntegerField
,则必须知道不能。
使用DateTimeField
代替PositiveIntegerField
进行设置。
我认为this是相关的,请查看更多信息。
答案 1 :(得分:0)
我明白了。我只需要重置迁移并删除db.sqlite文件,然后运行迁移并再次迁移即可。修复它!