2016-03-03 28 views
0

我使用Django 1.9Django會不會在一個應用程序創建了很多的一個模型表

無論出於何種原因,我根本無法獲得的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 

然後在外殼上,我試過如下:

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  

內容的`遷移/ 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')), 
      ], 
     ), 
    ] 
+0

「sites/migrations/0001_initial.py」的內容是什麼?您是否嘗試刪除所有* .pyc文件並重新開始? – Selcuk

+0

當你運行'python manage.py dbshel​​l'然後輸入'.tables'時你會得到什麼? – ChrisFreeman

+0

@Selcuk我刪除了包含在'products/migrations'文件夾中的所有文件,不知道你的意思是什麼* .pyc – thefoxrocks

回答

1

它看ms好像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 
相關問題