Skip to content

Instantly share code, notes, and snippets.

@rab
Last active October 4, 2017 23:56
Show Gist options
  • Save rab/9b63001d5f926468afdfa00248cf648e to your computer and use it in GitHub Desktop.
Save rab/9b63001d5f926468afdfa00248cf648e to your computer and use it in GitHub Desktop.
New bigint(20) cannot be FK reference to int(11)
# frozen_string_literal: true
# https://gist.github.com/rab/9b63001d5f926468afdfa00248cf648e
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "activerecord", "5.1.3"
# gem "sqlite3"
gem "mysql2"
end
require "active_record"
require "minitest/autorun"
require "logger"
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
# uh, ok, but what about a database-DEPENDENT bug report?
# ActiveRecord::Base.establish_connection(adapter: "mysql2", database: "???",
# username: "?", password: "?",
# host: "localhost",
# socket: "/tmp/mysql.sock",
# reconnect: false,
# pool: 5,
# encoding: "utf8"
# )
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
end
class CreateMember < ActiveRecord::Migration[5.0] # or any version that creates int(11) primary keys
def change
create_table :members do |t|
t.string :name
end
end
end
class CreateRole < ActiveRecord::Migration[5.1]
def change
create_table :roles do |t|
t.string :name, index: true, unique: true
t.string :description
end
end
end
class CreateHat < ActiveRecord::Migration[5.1]
def change
create_table :hats do |t|
t.references :member, foreign_key: true # probably just need THIS one and not roles at all
t.references :role, foreign_key: true
end
end
end
# class Member < ActiveRecord::Base
# end
# class Role < ActiveRecord::Base
# end
class Hat < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_normal_tables
CreateMember.migrate(:up)
CreateRole.migrate(:up)
CreateHat.migrate(:up)
rescue => oops
fail "Oops!"
else
Hat.reset_column_information
# For sqlite3, this would actually be "integer" and not be a problem
assert_equal "int(11)", Hat.columns.detect {|col| col.name == 'member_id'}.sql_type
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment