LoginSignup
4
3

More than 5 years have passed since last update.

postgresqlのMulticolumn

Last updated at Posted at 2017-10-16

Multicolumn(複合インデックス)を作成した時のこと

環境

  • Rails
  • PostgreSQL

設定

migrationファイルに以下を記述

add_index :xxxxxxxxxxx_xxxxxxx, [:xxxxxxxx_id, :xxxxxxxxxx_id]

Railsでよくあるindexの追記設定です。

記述後のmigrate

bundle exec rake db:migrate:up VERSION=xxxxxxxxxxxxxxxxxxxxxxxxx

上記コマンドでrailsのmigrateを実行

特にエラーも出力されず、無事にmigrationが完了

indexが無事にできているか確認

SELECT tablename, indexname FROM pg_indexes;
select * from pg_indexes where indexname = 'インデックス名';

上記コマンドを実施しても追加したはずのindexが表示されず

解決方法

PostgreSQlの識別子の長さは63バイトまでと判明

実際に自動生成される文字数を数えて見るとオーバーしてました。

[https://www.postgresql.jp/document/9.2/html/sql-syntax-lexical.html]

今回はたまたまテーブル名とカラム名が長くうまく生成されなかったみたいです。

migrate実行時にエラー出すとかこけて欲しかった。

オプションで:nameを指定して63バイト以内に指定しましょう。

add_index :xxxxxxxxxxx_xxxxxxx, [:xxxxxxxx_id, :xxxxxxxxxx_id], :name => 'original_name'

4
3
0

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
4
3