如何在不使用 db:migrate 的情况下将活动记录列类型从布尔值更改为整数?
我有一个这样的模型:
create_table :settings do |t|
t.integer :user_id
t.boolean :send_notification, :default => true
end
它运行良好,直到我需要指定多种类型的通知(电子邮件和/或短信),并且我希望用户能够指定他需要哪种通知。所以当我查看数据库中的设置表时,我认为这是可行的:
+----------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | MUL | NULL | |
| send_notification | tinyint(1) | YES | | 1 | |
+----------------------+------------+------+-----+---------+----------------+
所以我正在考虑使用位掩码重用 send_notification 列,例如 0 表示无任何内容,1 表示仅电子邮件,2 表示仅短信,3 表示短信和短信电子邮件。它在数据库中运行良好,但是当我在脚本/控制台中尝试它时。我意识到这是不可能的(布尔字段上的位屏蔽)。
ree > setting = Setting.first
=> #<Setting id: 1, user_id: 1, send_notification: false>
ree > setting.send_notification = 2
=> 2
ree > setting
=> #<Setting id: 1, user_id: 1, send_notification: false>
所以我需要改变列类型,但是由于我的表很大,所以成本有点高。除了创建迁移文件和 rake db:migration 之外,还有更好的解决方案吗?
I have a model like this:
create_table :settings do |t|
t.integer :user_id
t.boolean :send_notification, :default => true
end
It was running well until I need to specify multiple types of notification (email and/or sms) and I want the user to be able to specify which notification that he needs. So I thought it was doable when I look at my settting table in database:
+----------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | MUL | NULL | |
| send_notification | tinyint(1) | YES | | 1 | |
+----------------------+------------+------+-----+---------+----------------+
So I was thinking to reuse send_notification column with bit-masking, e.g. 0 means nothing, 1 means email only, 2 means sms only, and 3 means both sms and email. It was all working well in database, but when I tried it in script/console. I realized that it's not possible to do so (bit-masking on boolean field).
ree > setting = Setting.first
=> #<Setting id: 1, user_id: 1, send_notification: false>
ree > setting.send_notification = 2
=> 2
ree > setting
=> #<Setting id: 1, user_id: 1, send_notification: false>
So I need to alter the column type, but it's a bit costly as my table is very big. Is there any better solution other than creating migration file and rake db:migrating
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
布尔数据类型表示为 TINYINT(1),因此它是一个字节。如果
send_notification
字段用作布尔值,则应该有“0”- false、“1”-“true”或 NULL 值。如果有值> 1,它们可以用“1”更改 -现在,您可以使用此字段作为标志(NULL、0、1、2...)。如果需要,您可以更改表以将 TINYINT 更改为其他整数类型。
此外,MySQL 还有有用的BIT 函数。
Boolean data type is represented as TINYINT(1), so it is a byte. If the field
send_notification
was used as bool, there should be '0' - false, '1' - 'true' or NULL values. If there are valuse > 1, they can be changed with '1' -Now, you can use this field for your flags (NULL, 0, 1, 2...). If you want, you can alter table to change TINYINT to other integer type.
Also, MySQL has useful BIT functions.