所以我生成了一个迁移,将我的Reviews表的:comments
从字符串更改为文本。
class ChangeDataTypeForReviews < ActiveRecord::Migration
def self.up
change_table :reviews do |t|
t.change :comments, :text
end
end
def self.down
change_table :reviews do |t|
t.change :comments, :string
end
end
end
运行rake db:migrate
之后,它工作并确实将其更改为文本,但我想知道为什么在我的实际createReviews
迁移中,注释仍然是一个字符串,这是否重要,或者我应该把它改成文字?
class CreateReviews < ActiveRecord::Migration
def change
create_table :reviews do |t|
t.string :artist
t.string :comments
t.timestamps
end
end
end
答案 0 :(得分:1)
希望你已经运行了这个,
class ChangeDataTypeForReviews < ActiveRecord::Migration
def self.up
change_table :reviews do |t|
t.change :comments, :text
end
end
def self.down
change_table :reviews do |t|
t.change :comments, :string
end
end
end
执行后,
class CreateReviews < ActiveRecord::Migration
def change
create_table :reviews do |t|
t.string :artist
t.string :comments
t.timestamps
end
end
end
因此,最新的更改将反映在相应的表中。它不会更改先前创建的迁移文件中的任何内容。即,为什么我们将其称为Migration
,对吧?
因此,无需更改t.string :comments
,因为在最新迁移的情况下,我们确实将数据类型更改为text
。因此,它将始终仅被解释为text type
字段。
正确浏览the docs。
希望有所帮助:)