ruby-on-rails – rails中的迁移将varchar的1个字符转换为boolean

我有一个oracle数据库.在数据库中没有布尔值.但是在我迁移到rails之后,带有1个字符的VARCHARS变成了布尔值.如何将其再次转换为varchar?顺便说一句,我的数据库是oracle 10g,我正在使用最新版本的
ruby和rails.

 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 INTM_NO                                   NOT NULL NUMBER(12)
 INTM_NAME                                 NOT NULL VARCHAR2(240)
 CO_INTM_TYPE                              NOT NULL VARCHAR2(5)
 INTM_TYPE                                 NOT NULL VARCHAR2(2)
 TIN                                       NOT NULL VARCHAR2(20)
 CORP_TAG                                  NOT NULL VARCHAR2(1)
 SPECIAL_RATE                              NOT NULL VARCHAR2(1)
 LIC_TAG                                   NOT NULL VARCHAR2(1)
 MAIL_ADDR1                                NOT NULL VARCHAR2(50)
 MAIL_ADDR2                                         VARCHAR2(50)
 MAIL_ADDR3                                         VARCHAR2(50)
 BILL_ADDR1                                NOT NULL VARCHAR2(50)
 BILL_ADDR2                                         VARCHAR2(50)
 BILL_ADDR3                                         VARCHAR2(50)
 ISS_CD                                             VARCHAR2(2)
 PHONE_NO                                           VARCHAR2(40)
 BIRTHDATE                                          DATE
 CONTACT_PERS                                       VARCHAR2(50)
 DESIGNATION                                        VARCHAR2(5)
 PARENT_INTM_NO                                     NUMBER(12)
 CA_NO                                              VARCHAR2(15)
 LF_TAG                                             VARCHAR2(1)
 CO_INTM_NO                                         NUMBER(12)
 REF_INTM_CD                                        VARCHAR2(10)
 PAYT_TERMS                                         VARCHAR2(3)
 EFF_DATE                                           DATE
 EXPIRY_DATE                                        DATE
 USER_ID                                   NOT NULL VARCHAR2(8)
 LAST_UPDATE                               NOT NULL DATE
 REMARKS                                            VARCHAR2(4000)
 CPI_REC_NO                                         NUMBER(12)
 CPI_BRANCH_CD                                      VARCHAR2(2)
 WTAX_RATE                                          NUMBER(5,3)
 ACTIVE_TAG                                NOT NULL VARCHAR2(1)
 WHTAX_ID                                           NUMBER(5)
 INPUT_VAT_RATE                                     NUMBER(5,3)
 PRNT_INTM_TIN_SW                          NOT NULL VARCHAR2(1)
 OLD_INTM_NO                                        NUMBER(12)
 CA_DATE                                            DATE
 NICKNAME                                           VARCHAR2(40)
 CP_NO                                              VARCHAR2(40)
 EMAIL_ADD                                          VARCHAR2(50)
 FAX_NO                                             VARCHAR2(40)
 HOME_ADD                                           VARCHAR2(150)
 MASTER_INTM_NO                                     NUMBER(12)
 SUN_NO                                             VARCHAR2(40)
 SMART_NO                                           VARCHAR2(40)
 GLOBE_NO                                           VARCHAR2(40)
 PIN_CODE                                           VARCHAR2(6)

这是架构中的表

create_table "giis_intermediary", comment: "Intermediary (agent) information", primary_key: "intm_no", force: :cascade do |t|
    t.string  "intm_name",        limit: 240,                           null: false
    t.string  "co_intm_type",     limit: 5,                             null: false
    t.string  "intm_type",        limit: 2,                             null: false
    t.string  "tin",              limit: 20,                            null: false
    t.boolean "corp_tag",                                               null: false
    t.boolean "special_rate",                                           null: false
    t.boolean "lic_tag",                                                null: false
    t.string  "mail_addr1",       limit: 50,                            null: false
    t.string  "mail_addr2",       limit: 50,                                         
    t.string  "mail_addr3",       limit: 50,                                        
    t.string  "bill_addr1",       limit: 50,                            null: false
    t.string  "bill_addr2",       limit: 50,                                        
    t.string  "bill_addr3",       limit: 50,                                         
    t.string  "iss_cd",           limit: 2,                                          
    t.string  "phone_no",         limit: 40,                                        
    t.date    "birthdate",                                                         
    t.string  "contact_pers",     limit: 50,                                        
    t.string  "designation",      limit: 5
    t.integer "parent_intm_no",   limit: 12,   precision: 12,                     
    t.string  "ca_no",            limit: 15
    t.boolean "lf_tag"
    t.integer "co_intm_no",       limit: 12,   precision: 12
    t.string  "ref_intm_cd",      limit: 10
    t.string  "payt_terms",       limit: 3
    t.date    "eff_date"
    t.date    "expiry_date"
    t.string  "user_id",          limit: 8,                             null: false
    t.date    "last_update",                                            null: false
    t.string  "remarks",          limit: 4000
    t.integer "cpi_rec_no",       limit: 12,   precision: 12
    t.string  "cpi_branch_cd",    limit: 2
    t.decimal "wtax_rate",                     precision: 5,  scale: 3
    t.boolean "active_tag",                                             null: false
    t.integer "whtax_id",         limit: 5,    precision: 5
    t.decimal "input_vat_rate",                precision: 5,  scale: 3
    t.boolean "prnt_intm_tin_sw",                                       null: false
    t.integer "old_intm_no",      limit: 12,   precision: 12
    t.date    "ca_date"
    t.string  "nickname",         limit: 40
    t.string  "cp_no",            limit: 40
    t.string  "email_add",        limit: 50
    t.string  "fax_no",           limit: 40
    t.string  "home_add",         limit: 150
    t.integer "master_intm_no",   limit: 12,   precision: 12
    t.string  "sun_no",           limit: 40
    t.string  "smart_no",         limit: 40
    t.string  "globe_no",         limit: 40
    t.string  "pin_code",         limit: 6
  end

更新:我在github上查看了oracle增强适配器gem并找到了这些

# if emulate_booleans_from_strings then store booleans in VARCHAR2
      NATIVE_DATABASE_TYPES_BOOLEAN_STRINGS = NATIVE_DATABASE_TYPES.dup.merge(
        boolean: { name: "VARCHAR2", limit: 1 }
      )

Oracle增强适配器默认将只有1个字符的varchar更改为布尔值.

最佳答案 您遇到的问题是适配器.

由于Oracle没有本机布尔数据类型,因此默认情况下,activerecord-oracle_enhanced-adapter将尝试将特定字段中的某些值转换为布尔值.例如,以_yn结尾的字段会自动转换为布尔值.此外,限制为1的VARCHARS被视为布尔值.

您可以通过更改config / initializers / oracle.rb中的设置来解决此问题:

ActiveSupport.on_load(:active_record) do
  ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class_eval do
   self.emulate_booleans_from_strings = false
  end
end
点赞