Rails, ORA-000972 and You

| Comments

Lately I’ve been running up against “ORA-00972: identifier is too long”, when writing Rails migrations for Oracle. This particular error indicates that one of your identifiers is longer than 32 characters which is the maximum length for Oracle.

It’ll look something like this in your code:

1
2
3
4
5
6
7
8
9
class CreateLongTable < ActiveRecord::Migration
  def self.up
    create_table :long_table do |t|
      t.string :title
      t.references :someother_really_long_identifier
    end
  end
  ...
end

This migration will give you a foreign key id called someother_really_long_identifier_id which is obviously too long for Oracle. The solution is to use your own foreign key id name. In your migration add a shorter reference.

1
2
3
4
5
6
7
8
9
class CreateLongTable < ActiveRecord::Migration
  def self.up
    create_table :long_table do |t|
      t.string :title
      t.references :short_name
    end
  end
  ...
end

Then in your model use the shorter foreign key name.

1
2
3
4
5
class LongTable < ActiveRecord::Base
  belongs_to :someother_really_long_identifier,
             :class_name => 'ClassName',
             :foreign_key => 'short_name_id'
end

And everything should be sweet, your model will use someother_really_long_identifier and no-one need know that the table is really using short_name_id.