PG::StringDataRightTruncation: ERROR: PostgreSQL string(255) limit | Heroku

前端 未结 3 1518
名媛妹妹
名媛妹妹 2020-12-15 21:06

I Have a Listings controller and users can add a description. If the description is long, which it should be, I receive this error in Heroku:

A         


        
相关标签:
3条回答
  • 2020-12-15 21:19

    Do not edit your previous migrations. Never do that, as a rule. Instead you'll create a new migration that will make the change (and allow you to rollback if necessary.)

    First, generate the migration:

    rails g migration change_datatype_on_TABLE_from_string_to_text
    

    Then edit the generated file so that looks something like (change the table and column names as necessary):

    class ChangeDatatypeOnTableFromStringToText < ActiveRecord::Migration
      def up
        change_column :table_name, :column_name, :text, :limit => nil
      end
    
      def down
        change_column :table_name, :column_name, :string
      end
    end
    

    Now run the migration:

    bundle exec rake db:migrate
    

    That should do it. (Note I defined the up and down methods individually instead of using the change method because using the change method only works with reversible migrations, and trying to rollback a datatype change would throw a ActiveRecord::IrreversibleMigration exception.)

    0 讨论(0)
  • 2020-12-15 21:28

    The input given is too long for a string field, so just change to a text field:

    class ChangeListingsDescriptionTypeToText < ActiveRecord::Migration
      def change
        change_column :listings, :description, :text
      end
    end
    

    Then run rake db:migrate

    0 讨论(0)
  • 2020-12-15 21:33

    It would seem that specifying the column as a :text type and not a :string would fix this problem.

    0 讨论(0)
提交回复
热议问题