无法修复rails错误PG :: StringDataRightTruncation:错误:类型字符变化的值太长(255)

时间:2015-04-15 15:26:27

标签: ruby-on-rails ruby

我对铁轨并不是很熟悉,所以如果可能的话,我会很感激一些真正愚蠢的回答!

我得到了PG :: StringDataRightTruncation:ERROR:值太长,类型字符变化(255)错误。我在线阅读解决方案是创建一个迁移文件并将字段设置为:text然后:limit =>为零。

这些是我采取的步骤: 1. Rails g迁移 2.使用以下代码编辑迁移文件:

def up
    change_column :applications, :address, :text, :limit => nil
    change_column :applications, :mortAddress, :text, :limit => nil
    change_column :applications, :employer, :text, :limit => nil
    change_column :applications, :title, :text, :limit => nil
    change_column :applications, :empAddress, :text, :limit => nil
    change_column :applications, :coAddress, :text, :limit => nil
    change_column :applications, :coTitle, :text, :limit => nil
    change_column :applications, :coEmpAddress, :text, :limit => nil
    change_column :applications, :rrsp, :text, :limit => nil
    change_column :applications, :nonrrsp, :text, :limit => nil
    change_column :applications, :otherAssets, :text
  end

  def down
    change_column :applications, :address, :string
    change_column :applications, :mortAddress, :string
    change_column :applications, :employer, :string
    change_column :applications, :title, :string
    change_column :applications, :empAddress, :string
    change_column :applications, :coAddress, :string
    change_column :applications, :coTitle, :string
    change_column :applications, :coEmpAddress, :string
    change_column :applications, :rrsp, :string
    change_column :applications, :nonrrsp, :string
    change_column :applications, :otherAssets, :string
  end
end
  1. rake db:migrate
  2. 我还尝试删除:limit => nil for field:otherAssets,你可以从代码中看到。但是这仍然不起作用。

    我做错了吗?

2 个答案:

答案 0 :(得分:0)

class ChangeColumnType < ActiveRecord::Migration
  def up
    execute 'ALTER TABLE applications ALTER COLUMN address      TYPE text USING (address::text)'
    execute 'ALTER TABLE applications ALTER COLUMN mortAddress  TYPE text USING (mortAddress::text)'
    execute 'ALTER TABLE applications ALTER COLUMN employer     TYPE text USING (employer::text)'
    execute 'ALTER TABLE applications ALTER COLUMN title        TYPE text USING (title::text)'
    execute 'ALTER TABLE applications ALTER COLUMN empAddress   TYPE text USING (empAddress::text)'
    execute 'ALTER TABLE applications ALTER COLUMN coAddress    TYPE text USING (coAddress::text)'
    execute 'ALTER TABLE applications ALTER COLUMN coTitle      TYPE text USING (coTitle::text)'
    execute 'ALTER TABLE applications ALTER COLUMN coEmpAddress TYPE text USING (coEmpAddress::text)'
    execute 'ALTER TABLE applications ALTER COLUMN rrsp         TYPE text USING (rrsp::text)'
    execute 'ALTER TABLE applications ALTER COLUMN nonrrsp      TYPE text USING (nonrrsp::text)'
    execute 'ALTER TABLE applications ALTER COLUMN otherAssets  TYPE text USING (otherAssets::text)'
  end

  def down
    execute 'ALTER TABLE applications ALTER COLUMN address      TYPE string USING (address::string)'
    execute 'ALTER TABLE applications ALTER COLUMN mortAddress  TYPE string USING (mortAddress::string)'
    execute 'ALTER TABLE applications ALTER COLUMN employer     TYPE string USING (employer::string)'
    execute 'ALTER TABLE applications ALTER COLUMN title        TYPE string USING (title::string)'
    execute 'ALTER TABLE applications ALTER COLUMN empAddress   TYPE string USING (empAddress::string)'
    execute 'ALTER TABLE applications ALTER COLUMN coAddress    TYPE string USING (coAddress::string)'
    execute 'ALTER TABLE applications ALTER COLUMN coTitle      TYPE string USING (coTitle::string)'
    execute 'ALTER TABLE applications ALTER COLUMN coEmpAddress TYPE string USING (coEmpAddress::string)'
    execute 'ALTER TABLE applications ALTER COLUMN rrsp         TYPE string USING (rrsp::string)'
    execute 'ALTER TABLE applications ALTER COLUMN nonrrsp      TYPE string USING (nonrrsp::string)'
    execute 'ALTER TABLE applications ALTER COLUMN otherAssets  TYPE string USING (otherAssets::string)'
  end
end

答案 1 :(得分:0)

谢谢大家。我弄清楚问题是什么。我没有运行heroku运行db:migrate !!!!

问题解决了。