使用“。”更改列标题。期

时间:2016-07-13 20:34:19

标签: ruby-on-rails ruby

我使用.sql文件将数据导入Rails,以发现大多数列标题都有句号“。”在他们中。这显然是一个问题,在尝试运行更改列名称的迁移后,该期间仍然会将其丢弃。有任何变通方法吗?

class FixColumnNames < ActiveRecord::Migration
     def change
          change_table :my_table do |t|
               t.rename :p1.address, :'p1_address'
          end
     end
end

1 个答案:

答案 0 :(得分:3)

您还可以使用字符串:

with
     r ( r_id, r_len ) as (
       select r_id         , r_len from row_lengths union all
       select max(r_id) + 1, 4000  from row_lengths union all
       select max(r_id) + 2, null  from row_lengths
     ),
     b (str_id, str, r_id, token, prev_pos, new_pos) as (
       select str_id, txt || ' ', -1, null, null, 0 
         from input_strings
       union all
       select b.str_id, b.str, b.r_id + 1, 
              substr(str, prev_pos + 1, new_pos - prev_pos - 1), 
              b.new_pos, 
              new_pos + instr(substr(b.str, b.new_pos + 1, r.r_len + 1) , ' ', -1)
         from b join r
                on b.r_id + 2 = r.r_id           
     )
select   str_id, r_id, token, nvl(length(token), 0) as len
from     b
where    r_id > 0
order by str_id, r_id;

 STR_ID    R_ID TOKEN                                                LEN
------- ------- ------------------------------------------------ -------
      1       1 One Hundred Sixty-Nine Thousand Eight                 37
      1       2 Hundred Seventy-Four Dollars And Nine Cents           43
      2       1                                                        0
      2       2                                                        0
      3       1 Mathguy rules                                         13
      3       2                                                        0

6 rows selected.

以下语法也有效:

t.rename "p1.address", "p1_address"