Last active
March 8, 2019 16:30
-
-
Save alexcameron89/0300f554cf6cad2c9ef946f3aaf2d526 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
gem "rails", github: "rails/rails" | |
gem "pg" | |
end | |
require "active_record" | |
require "minitest/autorun" | |
require "logger" | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "rails") | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :posts, force: true do |t| | |
end | |
create_table :comments, force: true do |t| | |
t.integer :post_id | |
end | |
add_index :comments, :post_id, unique: true | |
end | |
class Post < ActiveRecord::Base | |
has_many :comments | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :post | |
end | |
class BugTest < Minitest::Test | |
def test_primary_key_growth | |
post_one = Post.create! | |
post_two = Post.create! | |
comment_one_post_one = Comment.create_or_find_by(post: post_one) | |
assert_equal 1, comment_one_post_one.id | |
# Create fails and it finds comment_one_post_one | |
comment_two_post_one = Comment.create_or_find_by(post: post_one) | |
assert_equal 1, comment_two_post_one.id | |
assert_equal comment_two_post_one, comment_one_post_one | |
# This fails | |
# Failure: | |
# BugTest#test_primary_key_growth [create_or_find_by.rb:56]: | |
# Expected: 2 | |
# Actual: #<Comment id: 3, post_id: 2> | |
comment_one_post_two = Comment.create_or_find_by(post: post_two) | |
assert_equal 2, comment_one_post_two.id | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment