2014-09-02 38 views
2

因此,我是Django的新手,我向model.py添加了一个字段,并在Django中创建了问题。在sqlite上南迁移失败后,我切换到MYSQL

我学会了我需要一个迁移工具,并用南。结果南出现问题与sqlite。所以我为MYSQL配置了Django设置。我可以将数据添加到数据库(MYSQL)

我删除了db.sqlite3文件,但它在syncDB之后回来。

当我运行执行syncdb这样说的:

Syncing... 
Creating tables ... 
Installing custom SQL ... 
Installing indexes ... 
Installed 0 object(s) from 0 fixture(s) 

Synced: 
> django.contrib.admin 
> django.contrib.auth 
> django.contrib.contenttypes 
> django.contrib.sessions 
> django.contrib.messages 
> django.contrib.staticfiles 
> south 

Not synced (use migrations): 
- accounts 
(use ./manage.py migrate to migrate these) 

所以我的问题是:1。 更改为MySQL并没有删除中期迁移是南失败的,因为我希望的那样。现在我有哪些选择,以应对这种

  1. 我连做任何事情,我也可以继续工作,我的应用程序或者是这个月中旬迁移的东西来解决?我问,因为新的MYSQL DB似乎有我添加的新领域,这就是为什么我想迁移的原因。所以你可以看到我的困惑......我的迁移破坏了,但现在使用MYSQL,这些字段没问题,但是Django仍然认为即将进行迁移。为什么是这样的,建议是什么?

感谢

+0

当我运行执行syncdb -all,即移除了“不同步”帐户并添加到“同步”。但是,如果我运行syncdb帐户报告为“未同步”再次。刚刚和FYI,我不知道这意味着什么,仍然需要建议。 Django新手。 – 2014-09-02 00:58:49

+0

哪个是你的Django版本? – AlvaroAV 2015-05-27 15:12:22

+0

谢谢@Liarez,但我已经从Django的移动上,因为它是比我更需要。在6个月内没有使用它。我的应用程序非常小。不管怎么说,还是要谢谢你。 – 2015-05-27 19:07:09

回答

0

我曾与数据库&迁移打很多很多很多(多)次&南......我希望这些信息可以帮助您解决您的问题,节省您的辛勤工作

几个小时

迁移帐户应用

  • 我有一个问题在这里,是accounts您的应用程序的名称?或者它是一个外部应用程序?

accounts应用程序似乎是由南非进行管理,这意味着它不会用命令python manage.py syncdb进行同步,则需要通过自己与迁移是:

# This command generates the migrations file 
python manage.py shchemamigration accounts --initial 
# This command use the migrations file and apply the changes to the DB 
python manage.py migrate accounts 


# --initial is used only in the INITIAL migration 
# If you modify the models on the accounts app and want to migrate again 
# You will need to 
python manage.py shchemamigration accounts --auto 
python manage.py migrate accounts 

留意您迁移

如果选中的文件夹migrations以下的标准结构是yourapp/yourapp/migrations

  • 如果删除数据库,记得删除迁移文件,是更好的,除非你需要保存数据库的变化
  • 确保您的数据库和迁移同步重新开始如果它们是同步增加一些对数据库的修改将很容易

  • 有些时候,虽然使用了南,当你做python manage.py syncdb您的应用程序同步,我将解释其问题可能来自这里:

    1. 您的应用程序模型的数据库命令python manage.py syncdb
    2. 1之后创建的。你做python manage.py schemamigration --initial,它会创建初始迁移现在
    3. ,如果你尝试做python manage.py migrate yourapp它会失败因为表已经创建,所以需要假初始迁移有:

      python manage.py migrate yourapp --fake 
      

      ,从这里,你可以使用以南没有问题

如何“重新启动”数据库

这个过程将清空数据库,并删除所有的迁移,如果您想要保存

有时你会惹迁移一些数据打理,并根据问题,它可以更快地再次删除数据库,并启动迁移:

python manage.py dbshell 

# Next 3 lines inside the MySQL console 
>> drop database yourappdatabase 
>> create database yourappdatabase 
>> quit 

# Delete all the migrations within the migrations folder 
rm yourapp/yourapp/migrations/00* 

python manage.py syncdb 
python manage.py schemamigration yourapp --initial 
python manage.py migrate yourapp 

如何从数据库

一段时间保存的信息■当你开始一个新的应用程序,你会删除/创建数据库多次,并避免对模型一再键入数据,它可能是有用的创建与某些型号迁移:

python manage.py dumpdata yourapp.ModelName > file.json 

要重新加载此数据再次,你只需要:

python manage.py loaddata file.json