加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 01:20:51 所属栏目:百科 来源:网络整理
导读:一会儿我运行了以下迁移: 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
一会儿我运行了以下迁移:
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读