Tag: rails migrations

Ruby on Rails:向现有数据库添加列

我收到一个错误: SQLite3::SQLException: no such column: ideas.list_id: SELECT “ideas”.* FROM “ideas” WHERE “ideas”.”list_id” = 2 但我补充道 t.integer :list_id 到我的数据库迁移文件: class CreateIdeas < ActiveRecord::Migration def change create_table :ideas do |t| t.string :name t.text :description t.string :picture t.timestamps end add_foreign_key :ideas, :lists end end 这给了我这个: class CreateIdeas < ActiveRecord::Migration def change create_table :ideas do |t| t.string :name t.text […]

为什么我不能在Rails的表中创建一个数组作为列?

为什么我不能这样做: class CreateModels < ActiveRecord::Migration def self.up create_table :fruit do |t| t.array :apples end end end 是否有其他方法可以使数组(“apples”)成为Fruit类实例的属性?

迁移数据 – 不仅仅是模式,Rails

有时,需要进行数据迁移。 随着时间的推移,使用域模型的代码更改和迁移不再有效,迁移失败。 迁移数据的最佳做法是什么? 我试过一个例子来澄清问题: 考虑一下。 你有一个迁移 class ChangeFromPartnerAppliedToAppliedAt < ActiveRecord::Migration def up User.all.each do |user| user.applied_at = user.partner_application_at user.save end end 当然,这完全没问题。 稍后,您需要更改架构 class AddAcceptanceConfirmedAt < ActiveRecord::Migration def change add_column :users, :acceptance_confirmed_at, :datetime end end class User < ActiveRecord::Base before_save :do_something_with_acceptance_confirmed_at end 对你来说没问题。 它完美运行。 但是,如果您的同事今天同时进行了这两项操作, 但尚未运行第一次迁移 , 那么在运行第一次迁移时会出现此错误: rake aborted! An error has occurred, this […]

编辑现有的Rails迁移是个好主意?

在开始一个新项目时,模型中有很多变化,我发现很容易编辑现有的迁移并运行db:clean或db:reset不是创建新的迁移。 当app没有达到生产时我就这样做了,这意味着我可以重置/清理数据库而不用担心,我正在独自工作或者是一个小团队的一部分。 但是今天,我在Rails指南中提到了以下建议,说它不是一个好主意并且不鼓励编辑现有的迁移: 编辑现有迁移并不是一个好主意:如果已在生产计算机上运行现有版本的迁移,您将为自己和同事创建额外的工作并导致严重的麻烦。 相反,您应该编写一个新的迁移来执行您需要的更改。 编辑尚未提交到源代码控制的新生成的迁移(或者更常见的是,尚未在开发计算机之外传播的迁移)相对无害。 我想知道: 我可能遇到什么潜在的陷阱? 这些陷阱是否适用于我的案例(开发阶段,独奏工作)?

如何在一个表中添加对同一模型的多个引用的迁移? 的Ruby / Rails

如何使用引用同一个表的两个字段创建迁移? 我有表A和图像。 A.image1_id将参考图像,而A.image2_id也将参考图像。 只有2张图片,不是很多。 如果我使用 class AddFields < ActiveRecord::Migration def change change_table(:ticket) do |t| t.references :image1_id t.references :image2_id end end end 我认为这不会起作用,因为它会在最后添加另一个_id,并且可能不会知道使用’image’模型。 我也想过 change_table(:ticket) do |t| t.references :image 但是,我该如何添加其中两个呢? 我也考虑过添加 create_table :images do |t| t.belongs_to :ticket t.string :file 但我只想要2,而不是很多,这似乎不允许从票证中获取图像,如ticket.image1或ticket.image2 。 根据这个文档http://apidock.com/rails/v3.2.8/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table这是我能找到的全部内容,t.references似乎也没有任何参数。 change_table(:suppliers) do |t| t.references :company end

Rails迁移:尝试将列的类型从字符串更改为整数

我在rails应用程序中使用rails generate migrations命令创建了一个表。 这是迁移文件: class CreateListings < ActiveRecord::Migration def change create_table :listings do |t| t.string :name t.string :telephone t.string :latitude t.string :longitude t.timestamps end end end 然后我想将纬度和经度存储为整数,所以我试图运行: rails generate migration changeColumnType 并且该文件的内容是: class ChangeColumnType < ActiveRecord::Migration def up #change latitude columntype from string to integertype change_column :listings, :latitude, :integer change_column :listings, :longitude, :integer #change longitude […]