Django不会在应用程序中为一个模型创建一个表

时间:2016-03-03 02:53:25

标签: python django

我正在使用Django 1.9

无论出于何种原因,我都无法让Django在我的products应用中为更多模型创建表格。我添加store模型并在admin.py上注册并运行manage.py makemigrations& manage.py migrate 无数次,我尝试添加我得到Operation error: no such table products_store的实例。

我有以下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

然后在shell上,我尝试了以下内容:

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]: []

对我来说似乎很奇怪。我还尝试删除所有迁移,然后再次运行所有迁移,但这似乎不起作用。

以下是迁移的输出:

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

更新  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     

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

1 个答案:

答案 0 :(得分:1)

似乎从数据库中删除了store表,并且迁移无法弄清楚如何将其添加回来。您始终可以在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