How to write SQL in a migration in Rails

后端 未结 4 1215
醉话见心
醉话见心 2021-01-01 08:41

I have the following SQL which I need to do

CREATE TABLE cars_users2 AS SELECT DISTINCT * FROM cars_users;

DROP TABLE cars_users;

ALTER TABLE cars_users2 R         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-01 09:09

    I prefer here doc:

    execute <<-SQL
      CREATE TABLE cars_users2 AS SELECT DISTINCT * FROM cars_users;
      DROP TABLE cars_users;
      ALTER TABLE cars_users2 RENAME TO cars_users;
    SQL
    

    notice: This only works for PostgreSQL, if you are using MySQL you should set CLIENT_MULTI_STATEMENTS for the adapter.

提交回复
热议问题