Exception: ActiveRecord::MismatchedForeignKey

Inherits:
StatementInvalid show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/errors.rb

Overview

Raised when a foreign key constraint cannot be added because the column type does not match the referenced column type.

Instance Attribute Summary

Attributes inherited from StatementInvalid

#binds, #sql

Instance Method Summary collapse

Constructor Details

#initialize(message: nil, sql: nil, binds: nil, table: nil, foreign_key: nil, target_table: nil, primary_key: nil, primary_key_column: nil) ⇒ MismatchedForeignKey

Returns a new instance of MismatchedForeignKey.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/errors.rb', line 184

def initialize(
  message: nil,
  sql: nil,
  binds: nil,
  table: nil,
  foreign_key: nil,
  target_table: nil,
  primary_key: nil,
  primary_key_column: nil
)
  if table
    type = primary_key_column.bigint? ? :bigint : primary_key_column.type
    msg = "      Column `\#{foreign_key}` on table `\#{table}` does not match column `\#{primary_key}` on `\#{target_table}`,\n      which has type `\#{primary_key_column.sql_type}`.\n      To resolve this issue, change the type of the `\#{foreign_key}` column on `\#{table}` to be :\#{type}.\n      (For example `t.\#{type} :\#{foreign_key}`).\n    EOM\n  else\n    msg = <<~EOM.squish\n      There is a mismatch between the foreign key and primary key column types.\n      Verify that the foreign key column type and the primary key of the associated table match types.\n    EOM\n  end\n  if message\n    msg << \"\\nOriginal message: \#{message}\"\n  end\n  super(msg, sql: sql, binds: binds)\nend\n".squish