Django 1.8.8迁移首先没有说什么,然后不会应用更改

时间:2016-01-26 16:05:25

标签: python mysql django

我相信这两者是相关的。

我遇到两个迁移问题:

  1. 我添加了一个包含两个新类(数据库表)的新文件。
  2. 这些表将添加到本地MySQL数据库(5.5)。
  3. 我有其他数据库的路由器,但不是我的本地MySQL(在1.5下开发)。
  4. 我正在运行Django 1.8.8(最近从1.6到1.7更新)和Python 2.7.6。
  5. 我遇到的第一个问题是Django没有意识到我在新文件中添加了新表。
  6. 我放弃了新文件(我有6个其他模型_ * .py文件,这个应用程序在南方工作得很好),只是将新类放在一个现有文件中。
  7. 现在makemigrations检测到更改,并表示已应用更改。但是新表没有出现在数据库中。
  8. 记录

    我的初始移民(在1.7升级期间完成):

    $ ./manage.py makemigrations -v 3 sanity
    Migrations for 'sanity':
      0001_initial.py:
    - Create model Alert_User_Criteria
    ...
    - Create model Test_Machine
    
    $ ./manage.py migrate -v 3 --fake sanity
    Operations to perform:
    Apply all migrations: sanity0
    Running pre-migrate handlers for application admin 
    ...
    Running migrations:
    Rendering model states... DONE (0.296s)
      Applying sanity.0001_initial... FAKED (0.010s)
    Running post-migrate handlers for application admin
    ...
    Running post-migrate handlers for application staff_status
    
    migrations/$ ls -al
    -rw-rw-r-- 1  21731 Jan 25 16:06 0001_initial.py
    -rw-rw-r-- 1   9953 Jan 25 16:06 0001_initial.pyc
    -rw-rw-r-- 1         0 Jan 14 12:14 __init__.py
    -rw-rw-r-- 1     142 Jan 25 16:06 __init__.pyc
    
    mysql> select * from  django_migrations;
    +----+---------------+---------------------------------------+---------------------+
    | id | app           | name                                  | applied             |
    + ----+---------------+---------------------------------------+---------------------+
    |  1 | contenttypes  | 0001_initial                          | 2016-01-12 11:56:34 |
    |  2 | auth          | 0001_initial                          | 2016-01-12 11:56:34 |
    |  3 | admin         | 0001_initial                          | 2016-01-12 11:56:34 |
    . . .
    | 30 | sanity        | 0001_initial                          | 2016-01-25 16:06:52 |
    +----+---------------+---------------------------------------+---------------------+
    

    新文件:MODELS_SA.PY

    from django.db   import models
    from django.conf import settings
    
    class Archive_Control(models.Model):
    config_name  = models.CharField(max_length=128, null=False, unique=True, db_index=True)
    is_active    = models.BooleanField(default=True)
    oldest_build = models.DateTimeField(auto_now_add=True)
    newest_build = models.DateTimeField(auto_now_add=True)
    archive_url  = models.CharField(max_length=128, null=True)
    
    def __unicode__(self):
        return self.config_name
    
    class Meta:
        app_label = 'sanity'
        ordering = ['config_name']
    
    class Build_Archives(models.Model):
    config         = models.ForeignKey(Archive_Control, db_index=True)
    gcid           = models.CharField(max_length=16, null=True)
    build_nbr      = models.CharField(max_length=16, null=True)
    build_url      = models.CharField(max_length=128, null=True)
    build_datetime = models.DateTimeField(null=True)
    
    def __unicode__(self):
        return self.config+" : "+str(self.build_datetime)
    
    class Meta:
        app_label = 'sanity'
        ordering = ['build_datetime']
        get_latest_by = 'build_datetime'
    
    def get_sortable_build_datetime(self):
        return self.build_datetime.strftime("%Y%m%d%H%M%S")
    def get_formatted_build_datetime(self):
        return self.build_datetime.strftime("%Y-%m-%d %H:%M:%S")
    
    build_datetime_sortable  = property(get_sortable_build_datetime)
    build_datetime_formatted = property(get_formatted_build_datetime)
    

    使用新文件运行MAKEMIGIGATION

    $ ./manage.py makemigrations -v 3 sanity
    No changes detected in app 'sanity'
    

    添加模型内容_A.PY TO MODELS.PY

    $ ./manage.py makemigrations -v 3 sanity
    Migrations for 'sanity':
      0002_archive_control_build_archives.py:
      - Create model Archive_Control
      - Create model Build_Archives
    

    为什么现在有效?

    $ ./manage.py migrate -v 3 sanity
    Operations to perform:
      Apply all migrations: sanity
    Running pre-migrate handlers for application admin
    . . .
    Running migrations:
      Rendering model states... DONE (0.275s)
      Applying sanity.0002_archive_control_build_archives... OK (0.072s)
    Running post-migrate handlers for application admin
    ...
    Running post-migrate handlers for application staff_status
    
    
    migrations$ ls -al
    total 64
    -rw-rw-r-- 1  21731 Jan 25 16:06 0001_initial.py
    -rw-rw-r-- 1    9953 Jan 25 16:06 0001_initial.pyc
    -rw-rw-r-- 1    1669 Jan 25 16:11 0002_archive_control_build_archives.py
    -rw-rw-r-- 1    1673 Jan 25 16:12 0002_archive_control_build_archives.pyc
    -rw-rw-r-- 1          0 Jan 14 12:14 __init__.py
    -rw-rw-r-- 1      142 Jan 25 16:06 __init__.pyc
    
    migrations$ cat 0002_archive_control_build_archives.py
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    
    from django.db import migrations, models
    
    class Migration(migrations.Migration):
    
    dependencies = [
    ('sanity', '0001_initial'),
    ]
    
    operations = [
    migrations.CreateModel(
        name='Archive_Control',
        fields=[
            ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
            ('config_name', models.CharField(unique=True, max_length=128, db_index=True)),
            ('is_active', models.BooleanField(default=True)),
            ('oldest_build', models.DateTimeField(auto_now_add=True)),
            ('newest_build', models.DateTimeField(auto_now_add=True)),
            ('archive_url', models.CharField(max_length=128, null=True)),
        ],
        options={
            'ordering': ['config_name'],
        },
    ),
    migrations.CreateModel(
        name='Build_Archives',
        fields=[
            ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
            ('gcid', models.CharField(max_length=16, null=True)),
            ('build_nbr', models.CharField(max_length=16, null=True)),
            ('build_url', models.CharField(max_length=128, null=True)),
            ('build_datetime', models.DateTimeField(null=True)),
            ('config', models.ForeignKey(to='sanity.Archive_Control')),
        ],
        options={
            'ordering': ['build_datetime'],
            'get_latest_by': 'build_datetime',
        },
    ),
    ]
    
    
    mysql> select * from  django_migrations;
    +----+---------------+---------------------------------------+---------------------+
    | id | app           | name                                  | applied             |
    +----+---------------+---------------------------------------+---------------------+
    . . .
    | 30 | sanity        | 0001_initial                          | 2016-01-25 16:06:52 |
    | 31 | sanity        | 0002_archive_control_build_archives   | 2016-01-25 16:12:17 |
    +----+---------------+---------------------------------------+---------------------+
    

    表格不在数据库中!

    我已经读过:

1 个答案:

答案 0 :(得分:0)

来自migrations上的文档:

  

内存中的结构也用于计算模型与迁移当前状态之间的差异; Django按照内存中的一组模型顺序完成所有更改,以便在上次运行makemigrations时提供模型的状态。然后,它会使用这些模型与 models.py文件中的模型进行比较,以计算出您已更改的内容。

(强调我的)

由于models_sa.py不是models.py文件,因此会被忽略,因此当您将其内容移动到models.py时,会应用其更改。

我意识到文档链接适用于较新版本但显示的原则相同