[英]Django: Cannot migrate database
I've spent the past hour scouring stack overflow and absolutely could not find anything that worked in my situation. 我花了过去一个小时来检查堆栈溢出,并且绝对找不到适合我的情况的任何东西。 I changed a ManytoManyField to a ForeignKeyField (to simplify my project) and it resulted in the error below.
我将ManytoManyField更改为ForeignKeyField(以简化我的项目),并导致以下错误。 I switched it back to a ManytoManyField but the error would not go away.
我将其切换回ManytoManyField,但错误不会消失。 I've stopped and started Postgres, dropped and recreated my tables but I cannot get it to work again.
我已经停止并启动Postgres,删除并重新创建了我的表,但是我无法使其重新工作。 My error is as follows:
我的错误如下:
Operations to perform:
Apply all migrations: admin, rango, sessions, contenttypes, auth
Running migrations:
Rendering model states... DONE
Applying rango.0013_auto_20180506_1225...Traceback (most recent call
last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/core/management/__init__.py", line 353, in
execute_from_command_line
utility.execute()
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/core/management/commands/migrate.py", line 200, in
handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake,
fake_initial=fake_initial)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/db/migrations/executor.py", line 121, in
_migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake,
fake_initial=fake_initial)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/db/migrations/executor.py", line 198, in
apply_migration
state = migration.apply(state, schema_editor)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state,
project_state)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/db/migrations/operations/fields.py", line 62, in
database_forwards
field,
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/db/backends/postgresql/schema.py", line 21, in
add_field
super(DatabaseSchemaEditor, self).add_field(model, field)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/db/backends/base/schema.py", line 382, in add_field
definition, params = self.column_sql(model, field,
include_default=True)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/db/backends/base/schema.py", line 145, in column_sql
default_value = self.effective_default(field)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/db/backends/base/schema.py", line 210, in
effective_default
default = field.get_db_prep_save(default, self.connection)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/db/models/fields/related.py", line 915, in
get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/db/models/fields/__init__.py", line 728, in
get_db_prep_save
prepared=False)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/db/models/fields/__init__.py", line 968, in
get_db_prep_value
value = self.get_prep_value(value)
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/django/db/models/fields/__init__.py", line 976, in
get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a
number, not 'datetime.datetime'
I am obviously not able to apply the migrations but my live server also says: 我显然不能应用迁移,但是我的实时服务器还说:
ProgrammingError at /admin/
relation "django_session" does not exist
LINE 1: ...ession_data", "django_session"."expire_date" FROM
"django_se...
This is what my models.py looks like: 这是我的models.py的样子:
from django.db import models
from django.template.defaultfilters import slugify
class Tag(models.Model):
name = models.CharField(max_length=128, unique = True)
views = models.IntegerField(default = 0)
likes = models.IntegerField(default = 0)
slug = models.SlugField(unique=True, blank = True)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Tag, self).save(*args, **kwargs)
def __str__(self):
return self.name
class Location(models.Model):
title = models.CharField(max_length=128, unique = True, blank = True)
class Photo(models.Model):
# location = models.ForeignKey(Location, blank = True)
# tag = models.ManyToManyField(Tag)
tag = models.ForeignKey(Tag)
title = models.CharField(max_length=128)
image = models.ImageField(upload_to = 'Stinagram/%Y/%m/%d')
views = models.IntegerField(default=0)
pub_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
My 0013_auto_20180506_1225 file is as follows: 我的0013_auto_20180506_1225文件如下所示:
# -*- coding: utf-8 -*-
# Generated by Django 1.9.10 on 2018-05-06 09:25
from __future__ import unicode_literals
import datetime
from django.db import migrations, models
import django.db.models.deletion
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
('rango', '0012_remove_photo_location'),
]
operations = [
migrations.RemoveField(
model_name='photo',
name='tag',
),
migrations.AddField(
model_name='photo',
name='tag',
field=models.ForeignKey(default=datetime.datetime(2018, 5,
6, 9, 25, 50, 146173, tzinfo=utc),
on_delete=django.db.models.deletion.CASCADE, to='rango.Tag'),
preserve_default=False,
),
]
Your ForeignKey field has a default of 您的ForeignKey字段的默认值为
datetime.datetime(2018, 5,
6, 9, 25, 50, 146173, tzinfo=utc)
in your migration file, you should fix that, according to your models file 在您的迁移文件中,您应该根据模型文件进行修复
tag = models.ForeignKey(Tag)
it should not have a default at all. 它完全没有默认值。 And it can't have a default which is a date anyway, because foreign key fields contain references to Django model objects, not to dates.
而且它也不能有默认的日期,因为外键字段包含对Django模型对象的引用,而不是日期。
So in your migration file you can change it to: 因此,您可以在迁移文件中将其更改为:
models.ForeignKey(to='rango.Tag')
see https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.ForeignKey 参见https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.ForeignKey
And also consider using some version control system so you can revert such hand-made app-breaking hacks (like the one which broke your migrations file) back to some working state in case you get in such places again... 并且还考虑使用某种版本控制系统,以便您可以将此类手工破坏应用程序的黑客(例如破坏了迁移文件的黑客)恢复到某些工作状态,以防万一您再次进入此类位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.