微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – 奇怪的Rails迁移/ schema.rb问题

一会儿我运行了以下迁移:
class CreatePipelinespecs < ActiveRecord::Migration
  def change
    create_table :pipeline_specs do |t|
      t.integer :id_no
      t.string :od
      t.string :wt
      t.string :material
      t.string :spec_type
      t.string :spec_grade
      t.string :mop
      t.string :stress_level
      t.string :joints
      t.text :notes
      t.string :ip
      t.references :pipeline,index: true,foreign_key: false

      t.timestamps null: false
    end
    add_index :pipeline_specs,:id_no
  end
end

我不知道现在发生了什么,但是每次运行rake db时,迁移scheme.rb文件将被更新:

create_table "pipeline_specs",force: :cascade do |t|
    t.integer  "id_no"
    t.string   "od"
    t.string   "wt"
    t.string   "material"
    t.string   "spec_type"
    t.string   "spec_grade"
    t.string   "mop"
    t.string   "stress_level"
    t.string   "joints"
    t.text     "notes"
    t.string   "ip"
    t.integer  "pipelines_id"
    t.datetime "created_at",null: false
    t.datetime "updated_at",null: false
  end

  add_index "pipeline_specs",["id_no"],name: "index_pipeline_specs_on_id_no",using: :btree
  add_index "pipeline_specs",["pipelines_id"],name: "index_pipeline_specs_on_pipelines_id",using: :btree

注意复数pipelines_id.实际的数据库表(dev,production等)都是pipeline_id,因为参考表是Pipeline是正确的.所以我添加一个新的不相关的迁移,并且schema.rb被更新,并且在我改回它们之后,这些更改再次变回复数.如果我在运行测试时忘记更改它们,那么错误的模式会被加载到测试环境中,所有的内容都会中断.

在这里亏了我在这里遗漏了一些明显的东西,还是有一些隐藏的迁移模式表等.

唯一认为我可以这样做的是当我使用原始迁移我使用管道:引用vs管道:引用,然后修复我的错误,然后在提交之前清理迁移并部署它.

任何想法在这里为什么这是发生和如何解决它一劳永逸

UPDATE

这是我的三个相关型号:

irb(main):031:0> Pipeline
=> Pipeline(id: integer,licence: string,company: string,company_id: integer,ba_code: string,substance_code: string,substance: string,h2s: string,partial_pressure: string,notes: text,created_at: datetime,updated_at: datetime,slug: string)
irb(main):032:0> Pipelinespec
=> Pipelinespec(id: integer,id_no: integer,od: string,wt: string,material: string,spec_type: string,spec_grade: string,mop: string,stress_level: string,joints: string,ip: string,pipeline_id: integer,slug: string)
irb(main):033:0> Pipelinesegment
=> Pipelinesegment(id: integer,line: integer,lsd_from: integer,sec_from: integer,twp_from: integer,rge_from: integer,m_from: integer,fc_from: string,lsd_to: integer,sec_to: integer,twp_to: integer,rge_to: integer,m_to: integer,fc_to: string,length: string,aasm_state: string,state_comment: string,state_user_id: integer,aasm_date: datetime,env: string,volume: string,pipeline_spec_id: integer,slug: string)

Pipeline has_many Pipelinespec和Pipelinesegment. Pipelinesegment has_one Pipelinespec.

更新2

检查了我的测试环境架构 – 没关系Ran rake db:migrate,并再次schema.rb更新.再次运行测试,并得到gobs:

ActiveRecord::StatementInvalid:         ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column "pipeline_id" of relation "pipeline_specs" does not exist
        LINE 1: ...,"mop","stress_level","joints","notes","ip","pipeline_...
                                                                     ^
        : INSERT INTO "pipeline_specs" ("id","id_no","od","wt","material","spec_type","spec_grade","pipeline_id","created_at","updated_at") VALUES (1,1,'88.9','3.18','S','Z245.1','359 2','9930','25','W','MyText','U','2017-04-24 03:47:26','2017-04-24 03:47:26')

因为灯具试图加载到刚刚在测试时刻加载的不正确的测试架构.

解决方法

我认为,因为您删除的迁移没有在测试环境中运行,请尝试通过以下方式重置测试数据库
RAILS_ENV=test rake db:migrate:reset

原文地址:https://www.jb51.cc/ruby/266083.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐