RoR: Cannot change_column in postgres, fine in MySQL (MySQL for development, Postgres on Heroku)

前端 未结 2 1899
长情又很酷
长情又很酷 2020-12-21 23:24

I had a column called \"prize\":

create_table :contests do |t|
  t.text :prize

I recently realized that this will always be an integer and

相关标签:
2条回答
  • 2020-12-22 00:14

    First you should use the same DB in both environments to prevent this kind of surprises.

    To run raw sql in migrations see this example http://guides.rubyonrails.org/migrations.html#using-the-up-down-methods

    0 讨论(0)
  • 2020-12-22 00:15

    I think you'll have to do it by hand:

    def up
        connection.execute(%q{
            alter table contests
            alter column prize type integer using cast(prize as integer),
            alter column price set default 200
        })
    end
    

    If you want to run the same migration in both MySQL and PostgreSQL then:

    def up
        case ActiveRecord::Base.connection
        when ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
            connection.execute(%q{
                alter table contests
                alter column prize type integer using cast(prize as integer),
                alter column price set default 200
            })
        when ActiveRecord::ConnectionAdapters::MySQLAdapter
            # MySQL version...
        end
    end
    

    Once you get around this problem, your next task will be to switch your development environment over to PostgreSQL so that you can start fixing all the other little problems (such as GROUP BY issues, case sensitive LIKE, column truncation behavior, ...) that you'll run into.

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