【问题标题】:Django won't create a table for one model of many in an appDjango 不会为应用程序中的多个模型创建表
【发布时间】:2016-06-16 04:42:18
【问题描述】:

我使用的是 Django 1.9

无论出于何种原因,我都无法让 Django 在我的products 应用程序中为更多模型创建表。添加store 模型并在admin.py 上注册并运行manage.py makemigrationsmanage.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')),
            ],
        ),
    ]

【问题讨论】:

  • sites/migrations/0001_initial.py的内容是什么?您是否尝试删除所有 *.pyc 文件并重新开始?
  • 运行python manage.py dbshell然后输入.tables会得到什么?
  • @Selcuk 我删除了包含在products/migrations 文件夹中的所有文件,不确定您所说的 *.pyc 是什么意思
  • @ChrisFreeman 查看更新的问题
  • @McLean25 我的意思是编译的 Python 文件,以扩展名 pyc 结尾。也尝试删除products/models.pyc

标签: python django


【解决方案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 

【讨论】:

    猜你喜欢
    • 2018-05-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多