You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I experienced this in MySQL, though it may show in other adapters. I did not see this in the SQLite adapter.
In this case the error reported when running this migration indicated that the problematic column was the bigint_id_model foreign key column, when it was actually the integer_id_model.
We seem to report the first column for other issues on the second column as well, this was just the first one I saw.
Please let me know if there is anything I can do to improve the self-executing test case as well. If it is preferred to handle database creation and dropping in the test I can do that as well 😄
Steps to reproduce
# frozen_string_literal: truerequire"bundler/inline"gemfile(true)dosource"https://rubygems.org"git_source(:github){ |repo| "https://github.com/#{repo}.git"}# Activate the gem you are reporting the issue against.gem"activerecord","6.1.0"gem"mysql2"endrequire"active_record"require"minitest/autorun"require"logger"ActiveRecord::Base.establish_connection(adapter: "mysql2",user: "root",database: "migration_error_issue_test")ActiveRecord::Base.logger=Logger.new(STDOUT)ActiveRecord::Schema.definedocreate_table:integer_id_models,id: falsedo |t|
t.integer:id,primary_key: trueendcreate_table:bigint_id_modelsdo |t|
endendclassCreateRelationshipModels < ActiveRecord::Migration[6.1]defchangecreate_table:relationship_modelsdo |t|
t.references:bigint_id_model,null: false,foreign_key: truet.references:integer_id_model,null: false,foreign_key: truet.timestampsendendendclassBugTest < Minitest::Testdeftest_migration_upCreateRelationshipModels.migrate(:up)rescue=>errorperrorrefute_includeserror.message,"bigint_id_model_id"assert_includeserror.message,"integer_id_model_id"endend
Expected behavior
The error reported should mention the integer_id_model reference having the wrong type.
Actual behavior
The error reported mentions the bigint_id_model reference having the wrong type.
System configuration
Rails version: 6.1.0
Ruby version: 2.6.3
The text was updated successfully, but these errors were encountered:
The issue was not even about the column types (this app is recent and all id columns are bigint), it was caused by the fact that the users table does not exist (the User model is backed by a table named secure_users, for reasons).
The error was:
== 20211222000747 CreatePurchases: migrating ==================================
-- create_table(:purchases)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:
Column `item_id` on table `purchases` does not match column `id` on `items`, which has type `bigint(20)`. To resolve this issue, change the type of the `item_id` column on `purchases` to be :bigint. (For example `t.bigint :item_id`).
Original message: Mysql2::Error: Cannot add foreign key constraint
[...]/db/migrate/20211222000747_create_purchases.rb:3:in `change'
Caused by:
ActiveRecord::MismatchedForeignKey: Column `item_id` on table `purchases` does not match column `id` on `items`, which has type `bigint(20)`. To resolve this issue, change the type of the `item_id` column on `purchases` to be :bigint. (For example `t.bigint :item_id`).
Original message: Mysql2::Error: Cannot add foreign key constraint
[...]/db/migrate/20211222000747_create_purchases.rb:3:in `change'
Caused by:
Mysql2::Error: Cannot add foreign key constraint
[...]/db/migrate/20211222000747_create_purchases.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
And the fix was to change foreign_key: true to foreign_key: { to_table: :secure_users } on the second line of the migration. The error message was not helpful at all for me to figure it out, this issue was (thanks @lelandmiller!).
I experienced this in MySQL, though it may show in other adapters. I did not see this in the SQLite adapter.
In this case the error reported when running this migration indicated that the problematic column was the bigint_id_model foreign key column, when it was actually the integer_id_model.
We seem to report the first column for other issues on the second column as well, this was just the first one I saw.
Please let me know if there is anything I can do to improve the self-executing test case as well. If it is preferred to handle database creation and dropping in the test I can do that as well 😄
Steps to reproduce
Expected behavior
The error reported should mention the
integer_id_model
reference having the wrong type.Actual behavior
The error reported mentions the
bigint_id_model
reference having the wrong type.System configuration
Rails version: 6.1.0
Ruby version: 2.6.3
The text was updated successfully, but these errors were encountered: