Django 1.9 - 创建新的app运行迁移,不创建表

时间:2016-11-10 09:12:49

标签: django sqlite

我添加了一个新应用并创建了一些模型。我已经运行makemigrations并迁移,所有都成功了。但是,当我打开sqlite表时,没有创建任何表。

来自模特的样本:

from __future__ import unicode_literals
from django.db import models
from django.conf import settings
import string
import random
import time
import os

# Create your models here.
from service.models import ServiceContacts


class Subnets(models.Model):
    subnet = models.GenericIPAddressField(protocol='IPv4',verbose_name="Subnet",blank=True,null=True)
    subnet_mask = models.CharField(max_length=4,verbose_name="Subnet Mask",choices=settings.SUBNET_MASK_CHOICES,blank=True)
    subnet_type = models.CharField(max_length=10 ,verbose_name='Subnet Type',choices=settings.SUBNET_TYPE_CHOICES,blank=True)

    class Meta:
        verbose_name = "Site Subnet Data"
        verbose_name_plural = "Site Subnet Data"

class SiteContacts(models.Model):
    name = models.CharField(max_length=200)
    title = models.CharField(max_length=200)
    mobile = models.CharField(max_length=200,blank=True, null=True)
    ddi = models.CharField(max_length=200,blank=True, null=True)
    notes = models.TextField(blank=True, null=True)

    class Meta:
        verbose_name = "Site Contact Data"
        verbose_name_plural = "Site Contact Data" 

来自初始的样本

# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-11-09 17:32
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import sites.models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('service', '0007_auto_20160701_0931'),
    ]

    operations = [
        migrations.CreateModel(
            name='CircuitFiles',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('circuit_file', models.FileField(blank=True, upload_to=sites.models.service_upload_path)),
                ('file_name', models.CharField(max_length=200, verbose_name='File Name')),
            ],
            options={
                'verbose_name': 'Circuit Files',
                'verbose_name_plural': 'Circuit Files',
            },
        ),
        migrations.CreateModel(
            name='CircuitNotes',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('date', models.DateField()),
                ('notes', models.TextField(blank=True)),
            ],
            options={
                'verbose_name': 'Circuit Notes',
                'verbose_name_plural': 'Circuit Notes',
            },
        ),
来自sqlite3db的

[root@network-tools infternal]# sqlite3 db.sqlite3
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
auth_group                   networks_circuitnotes
auth_group_permissions       networks_configtemplates
auth_permission              networks_configvariables
auth_user                    networks_majorsiteinfodata
auth_user_groups             networks_networkstock
auth_user_user_permissions   networks_networkstockusage
django_admin_log             networks_showroomconfigdata
django_content_type          networks_sitecontacts
django_migrations            networks_sitefiles
django_session               networks_sitefiletype
django_site                  networks_snmpdata
sqlite>

我应该在那里看到sites_subnets和sites_sitecontacts,以及其他很多。

迁移表中有以下内容,我在那里看不到网站| 0001_inital ......

100|networks|0069_configvariables_type|2016-11-03 15:17:41.424747
101|networks|0070_circuitinfodata_circuit_preference|2016-11-09 09:11:29.358213
102|networks|0071_auto_20161109_0915|2016-11-09 09:15:22.455639
103|networks|0072_auto_20161109_0916|2016-11-09 09:16:25.962542
104|sites|0002_auto_20161110_0859|2016-11-10 08:59:31.071382

已安装的应用:

INSTALLED_APPS = (
    'home.apps.HomeConfig',
    'oncall.apps.OncallConfig',
    'networks.apps.NetworksConfig',
    'sites.apps.SitesConfig',

进行迁移:

[root@network-tools infternal]# python manage.py makemigrations
Migrations for 'networks':
  0073_auto_20161110_1059.py:
    - Alter field circuit_type on circuitinfodata
Migrations for 'sites':
  0003_auto_20161110_1059.py:
    - Alter field circuit_type on circuits
[root@network-tools infternal]# python manage.py migrate
Operations to perform:
  Apply all migrations: service, sessions, admin, sites, auth, contenttypes, maintenance, oncall, networks
Running migrations:
  Rendering model states... DONE
  Applying networks.0073_auto_20161110_1059... OK
  Applying sites.0003_auto_20161110_1059...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/lib64/python2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/usr/lib64/python2.7/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/lib64/python2.7/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/lib64/python2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/lib64/python2.7/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/lib64/python2.7/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/usr/lib64/python2.7/site-packages/django/db/backends/base/schema.py", line 482, in alter_field
    old_db_params, new_db_params, strict)
  File "/usr/lib64/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 253, in _alter_field
    self._remake_table(model, alter_fields=[(old_field, new_field)])
  File "/usr/lib64/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 184, in _remake_table
    self.alter_db_table(model, temp_model._meta.db_table, model._meta.db_table)
  File "/usr/lib64/python2.7/site-packages/django/db/backends/base/schema.py", line 359, in alter_db_table
    "new_table": self.quote_name(new_db_table),
  File "/usr/lib64/python2.7/site-packages/django/db/backends/base/schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib64/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib64/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 323, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: sites_circuits

2 个答案:

答案 0 :(得分:0)

尝试使用以下过程迁移特定应用。

如果您创建初始迁移,然后运行migrate命令,有时迁移不完全,请尝试使用以下命令进行迁移

python manage.py makemigrations

创建初始迁移,然后使用应用名称

运行migrate命令
python manage.py migrate appname

希望这对你有帮助

答案 1 :(得分:0)

好的,我做了什么。

python manage.py sqlmigrate sites 0001

这给了我迁移的sql,然后运行

sqlite3 db.sqlite3

打开数据库,然后我只是将sqlmigrate命令的输出复制并粘贴到sql中,现在全部正常工作