我的用户表登录列是字符串类型,限制为 40 个字符。 现在我计划将限制增加到 55 个字符。
任何人请告诉我如何通过使用 ROR 迁移来增加此限制。
谢谢, 斯拉文
class YourMigration < ActiveRecord::Migration
def up
change_column :users, :login, :string, :limit => 55
end
def down
change_column :users, :login, :string, :limit => 40
end
end
class YourMigration < ActiveRecord::Migration
def change
change_column :users, :login, :string, :limit => 55
end
end
增加字符限制后,假设您插入一些长度超过 40 个字符的数据。
现在运行
db:rollback
,一些现有数据无法容纳 40 个字符:
为了避免此问题,我增加了
up
中的限制,但是,请勿更改 down
中的任何内容。
class FixUsersLogin < ActiveRecord::Migration
def up
change_column :users, :login, :string, limit: 55
end
def down
# do nothing in rollback
end
end