Alex McLean
Alex McLean

Reputation: 2764

Django won't create a table for one model of many in an app

I'm using Django 1.9

For whatever reason, I simply cannot get Django to create a table for any more models in my products app. After I added the store model and registered it on admin.py and running manage.py makemigrations & manage.py migrate countless times, I tried adding instances to which I got a Operation error: no such table products_store.

I have the following models.py:

from __future__ import unicode_literals

from django.db import models

# Create your models here.

def image_upload_location(instance, filename):
    print instance.name
    print filename
    return "static/images/products/%s" %(filename)

class Category(models.Model):
    title = models.CharField(max_length=120, unique=True)
    description = models.TextField(null=True,blank=True)

    def __unicode__(self):
        return self.title

class Product(models.Model):
    name = models.CharField(max_length = 120)
    description = models.TextField(blank=True,null=True)
    main_image = models.ImageField(upload_to=image_upload_location)
    price = models.DecimalField(decimal_places=2, max_digits=20)
    available = models.BooleanField(default=True)
    categories = models.ManyToManyField('Category', blank=True)

    def __unicode__(self):
        return self.name

class Store(models.Model):
    name = models.CharField(max_length=120)
    description = models.TextField(blank=True,null=True)

    def __unicode__(self):
        return self.name


class Building(models.Model):
    name = models.CharField(max_length=30)


class Variant(models.Model):
    variant_name = models.CharField(max_length=120)
    description = models.TextField(blank=True,null=True)
    variant_image = models.ImageField(upload_to=image_upload_location, null=True)
    price = models.DecimalField(decimal_places=2,max_digits=20)
    available = models.BooleanField(default=True)
    product = models.ForeignKey(Product)
    store = models.ForeignKey(Store)

    def __unicode__(self):
        return self.variant_name

Then on the shell, I tried the following:

In [1]: from products.models import Store

In [2]: from products.models import Product

In [3]: Store
Out[3]: products.models.Store

In [4]: Product
Out[4]: products.models.Product

In [5]: Store.objects.all()

OperationalError: no such table: products_store
In [8]: Product.objects.all()
Out[8]: []

Seems really strange to me. I also tried deleting all the migrations, and then running all the migrations again, but that didn't seem to work.

Here's the output from the migrations:

Migrations for 'products':
  0001_initial.py:
    - Create model Category
    - Create model Product
    - Create model Store
    - Create model Variant
A:try3 a$ python manage.py migrate
Operations to perform:
  Apply all migrations: sessions, admin, sites, auth, contenttypes, products
Running migrations:
  Rendering model states... DONE
  Applying sites.0001_initial... OK
  Applying sites.0002_alter_domain_unique... OK

Update: Output from manage.py dbshell

SQLite version 3.9.2 2015-11-02 18:31:45
Enter ".help" for usage hints.
sqlite> .tables
auth_group                   django_migrations          
auth_group_permissions       django_session             
auth_permission              django_site                
auth_user                    products_category          
auth_user_groups             products_product           
auth_user_user_permissions   products_product_categories
django_admin_log             products_variant           
django_content_type     

Contents of `migrations/0001_initial.py

class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Category',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(max_length=120, unique=True)),
                ('description', models.TextField(blank=True, null=True)),
            ],
        ),
        migrations.CreateModel(
            name='Product',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=120)),
                ('description', models.TextField(blank=True, null=True)),
                ('main_image', models.ImageField(upload_to=products.models.image_upload_location)),
                ('price', models.DecimalField(decimal_places=2, max_digits=20)),
                ('available', models.BooleanField(default=True)),
                ('categories', models.ManyToManyField(blank=True, to='products.Category')),
            ],
        ),
        migrations.CreateModel(
            name='Store',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=120)),
                ('description', models.TextField(blank=True, null=True)),
            ],
        ),
        migrations.CreateModel(
            name='Variant',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('variant_name', models.CharField(max_length=120)),
                ('description', models.TextField(blank=True, null=True)),
                ('variant_image', models.ImageField(null=True, upload_to=products.models.image_upload_location)),
                ('price', models.DecimalField(decimal_places=2, max_digits=20)),
                ('available', models.BooleanField(default=True)),
                ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='products.Product')),
                ('store', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='products.Store')),
            ],
        ),
    ]

Upvotes: 0

Views: 381

Answers (1)

ChrisFreeman
ChrisFreeman

Reputation: 6329

It seems as though the store table was dropped from the database and migrations cannot figure out how to add it back. You could always recreate the table in dbshell:

sqlite> .tables
auth_group                   django_migrations         
auth_group_permissions       django_session            
auth_permission              products_building            
auth_user                    products_category            
auth_user_groups             products_product             
auth_user_user_permissions   products_product_categories  
django_admin_log             products_variant             
django_content_type

sqlite> PRAGMA foreign_key s=OFF;
sqlite> BEGIN TRANSACTION;
sqlite> CREATE TABLE "products_store" ("id" integer NOT NULL PRIMARY KEY   AUTOINCREMENT, "name" varchar(120) NOT NULL, "description" text NULL);
sqlite> COMMIT;

sqlite> .tables
auth_group                  django_migrations         
auth_group_permissions      django_session            
auth_permission             products_building            
auth_user                   products_category            
auth_user_groups            products_product             
auth_user_user_permissions  products_product_categories  
django_admin_log            products_store               
django_content_type         products_variant 

Upvotes: 1

Related Questions