简体   繁体   English

Django缺少迁移文件-如何将数据库与主文件同步?

[英]Django missing migrations file - how to sync db with master file?

one of my fellow developers checked out from master and created new models for our website. 我的一位开发人员从大师那里签出,并为我们的网站创建了新模型。 He ran makemigrations, then ran migrate which obviously created the tables we wanted. 他进行了makemigrations,然后进行了迁移,显然创建了我们想要的表。

However, he never committed his changes to github and he altered the production database. 但是,他从未将更改提交到github,而是更改了生产数据库。 So when I went in to add a table today, when I ran makemigrations it the terminal listed several tables that I knew already existed...I was like "YOLO!" 因此,当我今天去添加一个表时,当我进行makemigrations时,终端会列出一些我已经知道存在的表...我就像“ YOLO!”。 and ran the migrate command anyways and it puked. 并无论如何都运行了migrate命令,然后将其删除。

So, since the migrations file isn't in my migrate folder, django thinks it needs to create those tables...then it goes to create them and pukes because they're already there. 因此,由于迁移文件不在我的迁移文件夹中,因此django认为需要创建这些表...然后创建它们并对其进行呕吐,因为它们已经存在。

The other developer is out of town visiting family and can't commit the file. 另一个开发人员不在城里探望家人,因此无法提交文件。

How do I get this set straight? 我该如何弄直呢? I think I need to run ./manage.py migrate my_app --fake 我想我需要运行./manage.py migration my_app --fake

But I don't completely understand what that does so I don't want to take the YOLO route and really mess things up... 但是我不完全了解它的作用,所以我不想走YOLO路线,把事情搞砸了……

OK, I promise everybody out there that I have been working on this problem for 9.5 hours today. 好的,我向所有人保证我今天已经在解决这个问题上工作了9.5个小时。 Turns out this was the answer: 原来这是答案:

Django migrations : relation already exists Django迁移:关系已经存在

However, there were some spelling/syntax errors that made it difficult to understand that this other person had the same problem as me. 但是,由于存在一些拼写/语法错误,因此很难理解另一个人和我有同样的问题。

to reiterate the solution: 重申解决方案:

  1. type: ./manage.py makemigrations your_app 类型:./manage.py makemigrations your_app
  2. Navigate to the my_app/migrations folder and open the migrations file that was just created (usually looks like '0005_modelsandstuff_blablabla.py" 导航到my_app / migrations文件夹,然后打开刚刚创建的迁移文件(通常看起来像'0005_modelsandstuff_blablabla.py”
  3. Delete the models that DON'T ALREADY EXIST. 删除尚未存在的模型。 Save the file and close 保存文件并关闭
  4. type: ./manage.py migrate your_app --fake 类型:./manage.py迁移your_app --fake
  5. this will then sync what is in the database now with your models schema without altering any of the actual database 然后,这将立即将数据库中的内容与您的模型架构同步,而无需更改任何实际数据库
  6. type: ./manage.py makemigrations your_app 类型:./manage.py makemigrations your_app
  7. type: ./manage.py migrate your_app 类型:./manage.py迁移your_app

And that's it! 就是这样! Everything is all synced up again. 一切都重新同步了。 Just as a quick jab I would like to say JavaScript sucks. 就像快速戳戳一样,我想说JavaScript很烂。 Thanks. 谢谢。

Following these steps should solve your problem. 执行以下步骤应该可以解决您的问题。

  1. Backup your database 备份数据库
  2. Stash your changes (so that only the missing schema changes are picked up) 存储您的更改(以便仅选择缺少的架构更改)
  3. Create the migrations (this creates the already applied schema changes) 创建迁移(这将创建已应用的架构更改)
  4. Run migrate with --fake (this will fake apply the already done schema changes) 使用--fake运行迁移(这将伪造已完成的架构更改)
  5. Apply your changes 套用您的变更
  6. Create the migrations 创建迁移
  7. Run migrate 运行迁移

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM