Aditya Singh
Aditya Singh

Reputation: 970

Unable to make migrations after changes in table for django 1.8

i added a field 'name' in the model
then i ran make migrations
then migrate but still it it doesnt detect the recent changes instead it is detecting old changes and then when i run migrate it says table already exists Why it is so?

here is models.py

class Category(MPTTModel):
    name = models.CharField(max_length =120 , null=True , blank=True)
    title = models.CharField(max_length =120)
    parent = models.ForeignKey('self' , null = True , blank = True , verbose_name='parent category', related_name='categories')
    description = models.TextField(null=True , blank=True)
    active = models.BooleanField(default=True)
    slug = models.SlugField(blank=True , unique=True)
    timestamp = models.DateTimeField(auto_now_add=True,auto_now=False)

    def get_absolute_url(self):
            return reverse('categories', kwargs={'path': self.get_path()})

here is the migration file

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings
import products.models


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Attribute',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('option', models.CharField(max_length=100, choices=[(b'color', b'color'), (b'size', b'size'), (b'Type', b'Type'), (b'Style', b'Style'), (b'sleeves', b'sleeves')])),
                ('value', models.CharField(max_length=100)),
            ],
        ),
        migrations.CreateModel(
            name='Brand',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('title', models.CharField(default=b'', max_length=50)),
                ('slug', models.SlugField(unique=True, blank=True)),
                ('active', models.BooleanField(default=True)),
                ('lft', models.PositiveIntegerField(editable=False, db_index=True)),
                ('rght', models.PositiveIntegerField(editable=False, db_index=True)),
                ('tree_id', models.PositiveIntegerField(editable=False, db_index=True)),
                ('level', models.PositiveIntegerField(editable=False, db_index=True)),
                ('parent', models.ForeignKey(blank=True, to='products.Brand', null=True)),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='BrandImage',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('is_slider', models.BooleanField(default=False)),
                ('is_featured', models.BooleanField(default=False)),
                ('is_logo', models.BooleanField(default=False)),
                ('active', models.BooleanField(default=True)),
                ('image', models.ImageField(upload_to=products.models.brand_upload_to)),
                ('brand', models.ForeignKey(related_name='brandimages', to='products.Brand')),
            ],
        ),
        migrations.CreateModel(
            name='Category',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=120, null=True, blank=True)),
                ('title', models.CharField(max_length=120)),
                ('description', models.TextField(null=True, blank=True)),
                ('active', models.BooleanField(default=True)),
                ('slug', models.SlugField(unique=True, blank=True)),
                ('timestamp', models.DateTimeField(auto_now_add=True)),
                ('lft', models.PositiveIntegerField(editable=False, db_index=True)),
                ('rght', models.PositiveIntegerField(editable=False, db_index=True)),
                ('tree_id', models.PositiveIntegerField(editable=False, db_index=True)),
                ('level', models.PositiveIntegerField(editable=False, db_index=True)),
                ('parent', models.ForeignKey(related_name='categories', verbose_name=b'parent category', blank=True, to='products.Category', null=True)),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='CategoryImage',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('image', models.ImageField(upload_to=products.models.category_upload_to)),
                ('category', models.ForeignKey(related_name='categoryimages', to='products.Category')),
            ],
        ),
        migrations.CreateModel(
            name='Filters',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('filtername', models.CharField(default=b'', max_length=200)),
                ('filtervalue', models.CharField(default=b'', max_length=200)),
                ('category', models.ForeignKey(related_name='category', to='products.Category')),
            ],
        ),
        migrations.CreateModel(
            name='Product',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('title', models.CharField(max_length=500)),
                ('gender', models.CharField(max_length=10, choices=[(b'Male', b'male'), (b'female', b'female'), (b'boys', b'boys'), (b'girls', b'girls')])),
                ('SKU', models.CharField(unique=True, max_length=100, blank=True)),
                ('description', models.TextField(max_length=500, null=True, blank=True)),
                ('price', models.IntegerField()),
                ('color', models.CharField(max_length=120)),
                ('discount', models.IntegerField(null=True, blank=True)),
                ('active', models.BooleanField(default=True)),
                ('is_related', models.BooleanField(default=False)),
                ('is_combo', models.BooleanField(default=False)),
                ('is_verified', models.BooleanField(default=False)),
                ('in_stock', models.BooleanField(default=False)),
                ('slug', models.SlugField(unique=True, blank=True)),
                ('timestamp', models.DateTimeField(auto_now_add=True)),
                ('brand', models.ForeignKey(related_name='products', to='products.Brand')),
                ('category', models.ForeignKey(related_name='products', default=b'', verbose_name=b'categories', to='products.Category')),
                ('parent', models.ForeignKey(related_name='children', blank=True, to='products.Product', null=True)),
            ],
        ),
        migrations.CreateModel(
            name='ProductAttribute',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=100)),
                ('value', models.CharField(max_length=100)),
                ('product', models.ForeignKey(to='products.Product')),
            ],
        ),
        migrations.CreateModel(
            name='ProductImage',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('image', models.ImageField(upload_to=products.models.image_upload_to)),
                ('product', models.ForeignKey(to='products.Product')),
            ],
        ),
        migrations.CreateModel(
            name='Rating',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('rating', models.IntegerField(null=True, blank=True)),
                ('verified', models.BooleanField(default=False)),
                ('product', models.ForeignKey(to='products.Product')),
                ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.CreateModel(
            name='Size',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=100)),
                ('value', models.CharField(max_length=100)),
                ('product', models.ForeignKey(to='products.Product')),
            ],
        ),
        migrations.AddField(
            model_name='filters',
            name='product',
            field=models.ForeignKey(to='products.Product'),
        ),
        migrations.AlterUniqueTogether(
            name='size',
            unique_together=set([('product', 'value')]),
        ),
        migrations.AlterUniqueTogether(
            name='product',
            unique_together=set([('SKU', 'slug', 'category', 'brand')]),
        ),
    ]

and when i run migrate this happens

django.db.utils.OperationalError: table "products_attribute" already exists

i cannot delete the db file and migrate as i have data in the tables.What do i do? Is there a way by which i save save my data somewhere?

Upvotes: 1

Views: 1030

Answers (1)

GwynBleidD
GwynBleidD

Reputation: 20539

If there are no performed migrations in history, solution is simple:

  1. Check structure of your tables in database
  2. Modify your models (undo some recent changes) so they will correspond to your database structure
  3. Remove all migrations (don't forget *.pyc files)
  4. run makemigrations on your app
  5. run migrate --fake-initial
  6. redo all changes to your models
  7. run makemigrations again
  8. run migrate

Upvotes: 2

Related Questions