1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

railsのmigrationファイル内で生のSQLを使う方法

Posted at

##やりたいこと
railsのmigrationファイル内で生のSQLを使いたい。

##環境
rails:6.0
mysql:5.7

##手順
まずは、migrationファイル作成

$rails g migration クラス名

作成されたmigrationファイルのdef内に記述

class クラス名 < ActiveRecord::Migration[6.0]
  def change
    sql = "ALTER TABLE users COLLATE 'utf8mb4_bin';"
    ActiveRecord::Base.connection.execute(sql)
  end
end

migration実行。

$rake db:migrate

以上です。

間違い等あればご指摘お願いします。

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?