我有一个包含以下字段的数据库 listingName,slug,property,city,place,ownerName,room,water,amenities,price,summary,phoneNumber,phone_image,email 我不想要phone_image字段,所以我从models.py中删除它并应用以下命令
python manage.py makemigrations rentals
python manage.py migrate
但我得到以下错误
OperationalError at /admin/rentals/rental/
no such column: rentals_rental.phone_image
所以我首先删除了我的数据库,然后应用命令python manage.py migrate但仍然得到相同的错误,虽然我的模型没有phone_image字段。
它在localhost中工作,但不在远程工作。因为我用nginx和gunicorn在数字海洋中托管我的应用程序。
admin.py
from django.contrib import admin
from rentals.models import Rental,Gallery
class InlineGallery(admin.TabularInline):
model = Gallery
fk_name = 'rental'
class RentalAdmin(admin.ModelAdmin):
inlines = [
InlineGallery,
]
admin.site.register(Rental,RentalAdmin)
admin.site.register(Gallery)
models.py
ownerName = models.CharField(_("Owner's Name"),max_length=255, blank=True,null=True,
help_text=_("Owner's Full Name"))
email = models.CharField(max_length=120,blank=True,null=True)
phoneNumber = models.PositiveIntegerField(blank=False,null=True,
help_text=_("Phone number of contact person"))
listingName = models.CharField(_("Lisitng Name"), max_length=255, blank=False,null=True,
help_text=_("Title of the rental space"))
slug = models.SlugField(unique=True,blank=True,null=True)
summary = models.TextField(max_length=500, blank=True,null=True,help_text=_("Description of the rental space"))
property = models.CharField(_("Property type"),max_length=10,null=True)
room = models.PositiveIntegerField(_("No of Rooms"), blank=False, null=True,
help_text=_("Number of bedrooms available"))
price = models.PositiveIntegerField(blank=False,null=True,
help_text=_("Rental price of the space per month"))
city = models.CharField(_("City"), max_length=255, blank=False,null=True,
help_text=_("City of the rental space"))
place = models.CharField(_("Place"), max_length=255, blank=False,null=True,
help_text=_("Place of the rental space"))
water = models.CharField(_("water facilities"),max_length=50,null=True,
help_text=_("Is there water facility?"))
amenities = models.CharField(_("amenities"),max_length=100,blank=True,null=True)
phone_image = models.CharField(blank=True, help_text='image form of the phone number', max_length=2048, null=True)
is_published = models.BooleanField(default=True)
created_on = models.DateTimeField(auto_now_add=True)
modified_on = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.listingName
迁移文件夹中有两个迁移文件
0001_initial.py
# -*- coding: utf-8 -*-
# Generated by Django 1.9.1 on 2016-03-30 14:23
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Gallery',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.ImageField(blank=True, null=True, upload_to='upload/')),
],
options={
'verbose_name_plural': 'Galleries',
'verbose_name': 'Gallery',
},
),
migrations.CreateModel(
name='Rental',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('ownerName', models.CharField(blank=True, help_text="Owner's Full Name", max_length=255, null=True, verbose_name="Owner's Name")),
('email', models.CharField(blank=True, max_length=120, null=True)),
('phoneNumber', models.PositiveIntegerField(help_text='Phone number of contact person', null=True)),
('listingName', models.CharField(help_text='Title of the rental space', max_length=255, null=True, verbose_name='Lisitng Name')),
('slug', models.SlugField(blank=True, null=True, unique=True)),
('summary', models.TextField(blank=True, help_text='Description of the rental space', max_length=500, null=True)),
('property', models.CharField(max_length=10, null=True, verbose_name='Property type')),
('room', models.PositiveIntegerField(help_text='Number of bedrooms available', null=True, verbose_name='No of Rooms')),
('price', models.PositiveIntegerField(help_text='Rental price of the space per month', null=True)),
('city', models.CharField(help_text='City of the rental space', max_length=255, null=True, verbose_name='City')),
('place', models.CharField(help_text='Place of the rental space', max_length=255, null=True, verbose_name='Place')),
('water', models.CharField(help_text='Is there water facility?', max_length=50, null=True, verbose_name='water facilities')),
('amenities', models.CharField(blank=True, max_length=100, null=True, verbose_name='amenities')),
('phone_image', models.CharField(blank=True, help_text='image form of the phone number', max_length=2048, null=True)),
('is_published', models.BooleanField(default=True)),
('created_on', models.DateTimeField(auto_now_add=True)),
('modified_on', models.DateTimeField(auto_now_add=True)),
],
options={
'verbose_name_plural': 'Rents',
'verbose_name': 'Rent',
},
),
migrations.AddField(
model_name='gallery',
name='rental',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='gallery', to='rentals.Rental', verbose_name='Rental'),
),
]
0002_remove_rental_phone_image.py
# -*- coding: utf-8 -*-
# Generated by Django 1.9.1 on 2016-04-20 02:01
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('rentals', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='rental',
name='phone_image',
),
]
我如何克服这个问题?我需要提供更多信息吗?
由于
答案 0 :(得分:1)
正如您在上面的答案中所说,该字段已从数据库中手动删除,因此迁移会尝试删除数据库中不再存在的字段。
通常,您不应该直接在数据库中删除字段,而是使用迁移。由于该字段已经消失,您现在只能伪造迁移以告知django已经进行了更改:
manage.py migrate rentals 0002 --fake
确保你处于移民状态0001,否则会被伪造。
为了确保您可以先运行以下命令:
manage.py migrate rentals 0001
答案 1 :(得分:0)
此类错误表示表phone_image
中没有任何名为rental
的列。因此,您的第二次迁移是尝试删除表rental
中不存在的列。不知何故,你从表中删除了该列,并且django迁移系统没有注意到它。
作为修复尝试以下选项之一:
1)删除0002_remove_rental_phone_image.py
并在0001_initial.py
删除此行:
('phone_image', models.CharField(blank=True, help_text='image form of the phone number'....))
,
并尝试应用迁移(如果它说:table rental is already exist
,尝试从数据库中DROP
;
2)删除所有迁移文件,然后重新创建数据库,进行迁移并应用它们。
答案 2 :(得分:0)
编写models.py代码时 然后运行命令
python manage.py makemigrations rentals
在此命令之后,您将看到0001_any_name.py,然后再次输入命令
python manage.py sqlmigrate rentals 0001 //0001 is prefix of above result
它将根据models.py创建您的数据库,然后您可以迁移
python manage.pt migrate
因此,当您第一次创建数据库时,此进程首次运行,但在任何数据库列中出现问题,然后上述过程再次重复。您的问题将解决。