【问题标题】:Migration from string to Boolean Rails [duplicate]从字符串迁移到布尔 Rails [重复]
【发布时间】:2020-05-28 04:37:25
【问题描述】:

我的一个表中有两列需要将其从字符串转换为布尔值。对象已经将数据附加到这些列(yes、no 或 nil),因此需要将 yes 结果转换为 true 并将 no/nil 转换为 false。

我正在为 change_column 运行一个简单的迁移,但这显然不是可行的方法,我无法找到解决方案。

我的表如下,两列分别是inner_sleeve和inserts。

create_table "listings", force: :cascade do |t|
    t.string "front_cover"
    t.bigint "version_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "back_cover"
    t.string "front_label"
    t.string "back_label"
    t.integer "step", default: 0
    t.string "cover_conditions", default: [], array: true
    t.string "cover_grading"
    t.text "cover_info"
    t.boolean "original_sleeve"
    t.string "disc_grading"
    t.text "disc_info"
    t.string "inner_sleeve"
    t.string "inserts"
    t.text "inner_inserts_info"
    t.text "other_info"
    t.bigint "user_id"
    t.string "disc_conditions", default: [], array: true
    t.integer "status", default: 0
    t.date "first_active_at"
    t.boolean "order_dispatched", default: false
    t.bigint "purchaser_id"
    t.index ["purchaser_id"], name: "index_listings_on_purchaser_id"
    t.index ["user_id"], name: "index_listings_on_user_id"
    t.index ["version_id"], name: "index_listings_on_version_id"
  end

谢谢!

【问题讨论】:

  • 这篇文章有帮助吗? stackoverflow.com/questions/26344257/…
  • 很遗憾没有。我收到以下错误:PG::DatatypeMismatch: ERROR: column "inner_sleeve" cannot be cast automatically to type boolean

标签: ruby-on-rails ruby postgresql boolean migration


【解决方案1】:

在这种情况下,我真的建议回退到好的 ol' SQL 来处理这类事情。举个例子,我在 PostgreSQL 中创建了一个小表:

convert_test=# select * from listings;
 id | inserts | inner_sleeve 
----+---------+--------------
  1 | yes     | no
  2 | yes     | 
  3 | no      | 
  4 |         | 
(4 rows)

然后我执行了两个原始 SQL 语句来更改列类型:

ALTER TABLE listings ALTER COLUMN inserts TYPE boolean USING CASE WHEN inserts = 'yes' THEN true ELSE false END;
ALTER TABLE listings ALTER COLUMN inner_sleeve TYPE boolean USING CASE WHEN inner_sleeve = 'yes' THEN true ELSE false END;

这应该是不言自明的,除了 USING 语句可能正是为了转换列中的值而运行的语句。

结果如预期:

convert_test=# select * from listings; id | inserts | inner_sleeve 
----+---------+--------------
  1 | t       | f
  2 | t       | f
  3 | f       | f
  4 | f       | f
(4 rows)

【讨论】:

    猜你喜欢
    • 2013-06-13
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    相关资源
    最近更新 更多