Django:无法迁移数据库

时间:2018-05-06 10:10:28

标签: django migration

我花了过去一小时的时间来搜索堆栈溢出,绝对找不到任何可行的方法。我将ManytoManyField更改为ForeignKeyField(以简化我的项目),并导致下面的错误。我将它切换回ManytoManyField,但错误不会消失。我已经停止并开始Postgres,删除并重新创建我的表,但我无法让它再次工作。我的错误如下:

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'

我显然无法应用迁移,但我的实时服务器也说:

ProgrammingError at /admin/
relation "django_session" does not exist
LINE 1: ...ession_data", "django_session"."expire_date" FROM 
"django_se...

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

我的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,
        ),
    ]

1 个答案:

答案 0 :(得分:0)

您的ForeignKey字段的默认值为

datetime.datetime(2018, 5, 
6, 9, 25, 50, 146173, tzinfo=utc)

在您的迁移文件中,您应根据模型文件

修复此问题
tag = models.ForeignKey(Tag)

根本不应该有默认值。它不能有一个默认的日期,因为外键字段包含对Django模型对象的引用,而不是日期。

因此,在您的迁移文件中,您可以将其更改为:

models.ForeignKey(to='rango.Tag')

请参阅https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.ForeignKey

并且还考虑使用一些版本控制系统,这样你就可以将这些手工制作的应用程序破解黑客(比如破坏你的迁移文件的那些)恢复到一些工作状态,以防你再次进入这些地方...... / p>