0

I had a database with following fields listingName,slug,property,city,place,ownerName,room,water,amenities,price,summary,phoneNumber,phone_image,email I didn't want phone_image field so i removed it from models.py and applied the following command

python manage.py makemigrations rentals
python manage.py migrate

But i get below error

OperationalError at /admin/rentals/rental/
no such column: rentals_rental.phone_image

so i deleted my database first and then applied the command python manage.py migrate but still getting the same error though my models do not have phone_image field.

It works in localhost but not in remote. As i have hosted my application in digital ocean with nginx and 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

I have two migrations file inside migration folder

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',
    ),
]

How can i overcome this issue? Do i need to provide any more information?

Thanks

pri
  • 620
  • 2
  • 9
  • 20
  • I noticed that in the first migration it sais `model_name='Rental'` but in the second it sais `model_name='rental'`(lower case). did you rename your model by any chance? – Tim Apr 20 '16 at 07:35
  • No i have not changed the model name. It is Rental. – pri Apr 20 '16 at 07:45
  • Please post your `admin.py` – Selcuk Apr 20 '16 at 07:51
  • Check your database if the field `phone_image` is available in the table `rental` if not, it got removed somehow. – Tim Apr 20 '16 at 07:56
  • @TimSchneider The OP intentionally removed it from the model. – Selcuk Apr 20 '16 at 07:59
  • I updated my question with admin.py – pri Apr 20 '16 at 08:01
  • Ok, maybe I interpreted the question wrong. It might not be a problem of the migrations after all. Did the `manage.py migrate` command work without problems? – Tim Apr 20 '16 at 08:13
  • Did you restart your django instance after the migration? – Tim Apr 20 '16 at 08:30
  • Yes. it did not work so i added phone_image fields back in models.py and it worked. I will update my models.py and migrations files. Please let me know what steps i apply to remove phone_image field without any error. – pri Apr 20 '16 at 08:35
  • you said that it works on localhost but on remote machine it doesn't work right? Did you removed migration files from that remote machine? – sehrob Apr 20 '16 at 09:17
  • when it did not work i removed it. But now i have copied to previous state. – pri Apr 20 '16 at 09:25
  • its working now with phone_image field after i have revert back to previous state of my models which i have updated in my question. Now i want to remove phone_image field. So can i remove phone_image field from the models and then apply makemigrations rentals and migrate command respectively ? – pri Apr 20 '16 at 09:27

3 Answers3

1

As you said in the answers above, the field got manually removed from the database, so the migration tries to delete a field that is no longer existing in the database.

Normally you should never delete fields directly in the db but use the migrations instead. As the field is already gone you can now only fake the migration to tell django that the changes were already made:

manage.py migrate rentals 0002 --fake

Make sure though that you are at migration 0001, otherwise that would be faked aswell.

just to be sure, you could run the following command first:

manage.py migrate rentals 0001
Tim
  • 1,272
  • 11
  • 28
0

This type of error means that, there is no any column named phone_image in the table rental. So, your second migration is trying to remove a column that is not present in the table rental. Somehow, you removed that column from the table, and django migration system didn't notice it.

As a fix try one of these options:

1) delete 0002_remove_rental_phone_image.py and in 0001_initial.py remove this line: ('phone_image', models.CharField(blank=True, help_text='image form of the phone number'....)), and try to apply migrations (if it says: table rental is already exist, try to DROP it from the database);

2) delete all your migration files, and recreate the database, make migrations and apply them.

sehrob
  • 1,034
  • 12
  • 24
  • This could lead to data loss which could be a problem. – Tim Apr 20 '16 at 07:55
  • yes, but @pri says: `deleted my database first` that's why I have proposed this option. – sehrob Apr 20 '16 at 08:00
  • I deleted my migration files and also db.sqlite3 and applied python manage.py migrate but did not work either – pri Apr 20 '16 at 08:03
  • Like @TimSchneider said, it could lead to data loss but this is my first attempt and there is no any data yet. However it might be critical issue once i have data in my database which i dont want to loose. – pri Apr 20 '16 at 08:05
  • I have already tried both options you stated but none of these worked for me. – pri Apr 20 '16 at 08:06
0

When you write your code of models.py then Run Command

python manage.py makemigrations rentals

after this command you will see 0001_any_name.py then Enter again command

python manage.py sqlmigrate rentals 0001    //0001 is prefix of above result

It will create your database accouding to models.py then you can migrate

python manage.pt migrate

So this process run first time when you first time create database but in any database column problem occurs then this above procedure repeat again.your problem will solve.

Neeraj Kumar
  • 3,851
  • 2
  • 19
  • 41