django中的makemigrations是否因某种原因重新创建现有表?

时间:2016-06-14 15:09:31

标签: django

我想验证django 1.8中的makemigrations是否存在使用sql lite的错误,或者我做错了什么。

  1. 删除数据库并删除所有迁移文件夹后。我跑
  2. python manage.py makemigrations 
    python manage.py migrate
    

    创建DB没有任何问题。

    2.我必须在其中一个应用程序中修改现有模型(app abc) 我执行我的更改并再次运行

    python manage.py makemigrations 
    

    3.它没有找到任何变化

    然后我再次运行相同的东西,但使用应用名称

    python manage.py makemigrations abc
    

    4.它在迁移中做了一些更新,我相信它会重新创建所有表格,而不仅仅是我的更改 !!!!

    5.然后我执行

      

    python manage.py migrate

    并获得该表已存在的错误。

    这是django框架中的一个错误,还是我做错了什么,并且有这样的原因?

    从第2步开始从我的shell复制粘贴:

        (mrp) C:\Users\I812624\dev\mrp\src>python manage.py makemigrations
    No changes detected
    
    (mrp) C:\Users\I812624\dev\mrp\src>python manage.py makemigrations purchase
    Migrations for 'purchase':
      0001_initial.py:
        - Create model PO
        - Create model POmaterial
    
    (mrp) C:\Users\I812624\dev\mrp\src>python manage.py migrate
    Operations to perform:
      Synchronize unmigrated apps: customer, manufacture, product, django_filters, a
    utofixture, staticfiles, messages, smart_selects, watson, sales, item, django_co
    untries, mptt, inventory, django_select2, production, main, crispy_forms
      Apply all migrations: purchase, vendor, sessions, admin, sites, flatpages, con
    tenttypes, auth, registration
    Synchronizing apps without migrations:
      Creating tables...
        Running deferred SQL...
      Installing custom SQL...
    Running migrations:
      Rendering model states... DONE
      Applying purchase.0001_initial...Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\management\__init
    __.py", line 338, in execute_from_command_line
        utility.execute()
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\management\__init
    __.py", line 330, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\management\base.p
    y", line 390, in run_from_argv
        self.execute(*args, **cmd_options)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\management\base.p
    y", line 441, in execute
        output = self.handle(*args, **options)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\management\comman
    ds\migrate.py", line 221, in handle
        executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\migrations\executor
    .py", line 110, in migrate
        self.apply_migration(states[migration], migration, fake=fake, fake_initial=f
    ake_initial)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\migrations\executor
    .py", line 147, in apply_migration
        state = migration.apply(state, schema_editor)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\migrations\migratio
    n.py", line 115, in apply
        operation.database_forwards(self.app_label, schema_editor, old_state, projec
    t_state)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\migrations\operatio
    ns\models.py", line 59, in database_forwards
        schema_editor.create_model(model)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\backends\base\schem
    a.py", line 282, in create_model
        self.execute(sql, params or None)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\backends\base\schem
    a.py", line 107, in execute
        cursor.execute(sql, params)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\backends\utils.py",
     line 79, in execute
        return super(CursorDebugWrapper, self).execute(sql, params)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\backends\utils.py",
     line 64, in execute
        return self.cursor.execute(sql, params)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\utils.py", line 97,
     in __exit__
        six.reraise(dj_exc_type, dj_exc_value, traceback)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\backends\utils.py",
     line 62, in execute
        return self.cursor.execute(sql)
      File "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\backends\sqlite3\ba
    se.py", line 316, in execute
        return Database.Cursor.execute(self, query)
    django.db.utils.OperationalError: table "purchase_po" already exists
    

1 个答案:

答案 0 :(得分:0)

回答我自己的问题。

所以我想通了。我认为问题在于我最初删除了文件夹迁移,当我在没有指定特定应用程序的情况下运行makemigrations时它确实创建了一个数据库,但它在其中创建了包含____init____.py的文件夹makemigrations。

解决方案每次删除db并删除迁移文件夹时,无论出于何种原因,都不要删除迁移文件夹,只删除其内容,不包括init文件。 或者当你删除文件夹时,运行为@MicroPyramid建议的每个应用程序单独进行迁移。

没有进行更深入的调查,它看起来像是混乱的设计行为或Django方面的错误。