简体   繁体   English

Rails 每次重启服务器都需要迁移

[英]Rails need to migrate everytime restart server

When deploying my app to heroku , i get some error about migration.将我的应用程序部署到heroku ,我收到一些有关迁移的错误。

So that I combine all migration, just hold create table file contain all finally property, delete edit table.这样我就合并了所有的迁移,只需要创建包含所有finally 属性的表文件,删除编辑表。

Now, everytime restart server(localhost), i have to db:migrate again to everything work.现在,每次重新启动服务器(本地主机)时,我都必须再次db:migrate才能正常工作。

When i just use db:migrate , the error said: table 'product' already existed' So i have to db:drop first, then db:migrate to make my app work It took me a lot of time.当我只使用db:migrate ,错误说: table 'product' already existed'所以我必须先db:drop ,然后db:migrate才能使我的应用程序工作这花了我很多时间。 How can I resolve that issue?我该如何解决这个问题? Here is my migrate status:这是我的迁移状态:

up 20190306060445 ********** NO FILE ********** down 20190306060545 Create product up 20190307035103 Create active storage tablesactive storage up 20190308045037 ********** NO FILE ********** up 20190308071304 ********** NO FILE ********** down 20190308072304 Devise create users up 20190308074025 Devise create admins down 20190308082018 Create categories up 20190308083018 ********** NO FILE ********** up 20190311041416 ********** NO FILE ********** up 20190313084429 Create payment up 20190314024236 Create cart up 20190314024911 Create cart product up 20190314035334 Create messages up 20190314145333 Create payment item up 20190314151340 ********** NO FILE ********** up 20190315020606 ********** NO FILE ********** up 20190318073652 Create voucher up 20190319035819 ********** NO FILE ********** up 20190319134032 Contacts up 20190320034249 ********** NO FILE ********** up 20190320062223 ********** NO FILE ********** up 20190325132236 ********** NO FILE ********** up 20190325155829 ********** NO FILE ********** up 20190329095529 ********** NO FILE ********** up 20190329110656 ********** NO FILE ********** up 20190330145000 ********** NO FILE ********** up 20190331162926 ********** NO FILE ********** up 20190408155326 ********** NO FILE ********** up 20190419055934 ********** NO FILE ********** up 20190419060341 ********** NO FILE ********** up 20190502105847 ********** NO FILE ********** up 20190502110019 Add stock up 20190506114910 ********** NO FILE ********** up 20190506115146 ********** NO FILE ********** up 20190506115302 ********** NO FILE ********** up 20190513042021 ********** NO FILE ********** up 20190513050115 ********** NO FILE ********** up 20190514025220 ********** NO FILE ********** up 20190514031046 ********** NO FILE ********** up 20190516010239 Create comment up 20190516020021 ********** NO FILE ********** up 20190517132706 Create notification up 20190524152143 Create brands up 20190527040142 ********** NO FILE ********** up 20190528113143 Add columns devise up 20190528113658 Add columns devise 1 up 20190531125307 Create news up 20190531144908 Create ckeditor assets up 20190601100118 Create news products up 20190602144319 Create type of news up 20190602155208 ********** NO FILE ********** up 20190603062824 ********** NO FILE ********** up 20190606172629 ********** NO FILE ********** up 20190607074100 ********** NO FILE ********** up 20190607122350 ********** NO FILE ********** up 20190607140214 ********** NO FILE ********** up 20190608082804 ********** NO FILE ********** up 20190609051100 Add omniauth to users up 20190611115405 ********** NO FILE ********** up 20190612071933 Create table carousels up 20190612100433 ********** NO FILE ********** up 20190613082404 ********** NO FILE ********** up 20190614131413 ********** NO FILE ********** up 20190615161828 ********** NO FILE ********** up 20190617154212 Create district up 20190617154602 Create province up 20190619140034 Create ward up 20190620134528 ********** NO FILE ********** down 20190620140214 Convert tables to utf8 v1 up 20190622123753 ********** NO FILE **********

In changing all your migrations you've created "new" migrations.在更改所有迁移时,您已经创建了“新”迁移。 rails db:migrate is trying to apply these "new" migrations to an already existing database. rails db:migrate正在尝试将这些“新”迁移应用到已经存在的数据库中。 (The list of applied migrations is held in the schema_migrations table). (已应用的迁移列表保存在schema_migrations表中)。 We can see all the down migrations are the ones which have not been applied.我们可以看到所有的down迁移都是没有被应用的。 Including down 20190306060545 Create product .包括down 20190306060545 Create product

Since this is a running production database, presumably the product table already exists.由于这是一个正在运行的生产数据库,大概product表已经存在。 When Rails runs migration 20190306060545 Create product which presumably tries to create_table :product it will fail because product already exists.当 Rails 运行迁移20190306060545 Create product可能尝试create_table :product ,它将失败,因为product已经存在。

One way to avoid this is to first check if the table exists in your "create table" migrations.避免这种情况的一种方法是首先检查该表是否存在于您的“创建表”迁移中。 If it does, do nothing.如果是,什么都不做。

if table_exists?(:product)
  return
end

To reproduce this in development...为了在开发中重现这一点......

  • Check out the original migrations.查看原始迁移。
  • Create a fresh schema from that: rails db:migrate:reset从中创建一个新模式: rails db:migrate:reset
  • Check out your new combined migrations.查看您的新组合迁移。
  • Try to migrate: rails db:migrate尝试迁移: rails db:migrate

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

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