Skip to content

Instantly share code, notes, and snippets.

@x2764tech
Created February 4, 2015 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x2764tech/4ff7c015cd5d7137c295 to your computer and use it in GitHub Desktop.
Save x2764tech/4ff7c015cd5d7137c295 to your computer and use it in GitHub Desktop.
Rails Query by specified primary key issue
-- create_table(:posts, {:force=>true})
D, [2015-02-04T09:40:42.602488 #22036] DEBUG -- :  (0.2ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "system_id" integer) 
-> 0.0041s
-- create_table(:comments, {:force=>true})
D, [2015-02-04T09:40:42.602859 #22036] DEBUG -- :  (0.1ms) CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "post_id" integer)
-> 0.0003s
Run options: --seed 15155
# Running:
D, [2015-02-04T09:40:42.621038 #22036] DEBUG -- :  (0.0ms) begin transaction
D, [2015-02-04T09:40:42.625989 #22036] DEBUG -- : SQL (0.1ms) INSERT INTO "posts" ("system_id") VALUES (?) [["system_id", 23]]
D, [2015-02-04T09:40:42.626287 #22036] DEBUG -- :  (0.0ms) commit transaction
D, [2015-02-04T09:40:42.632388 #22036] DEBUG -- :  (0.0ms) begin transaction
D, [2015-02-04T09:40:42.632792 #22036] DEBUG -- : SQL (0.0ms) INSERT INTO "comments" DEFAULT VALUES
D, [2015-02-04T09:40:42.632980 #22036] DEBUG -- :  (0.0ms) commit transaction
D, [2015-02-04T09:40:42.633107 #22036] DEBUG -- :  (0.0ms) begin transaction
D, [2015-02-04T09:40:42.634920 #22036] DEBUG -- : SQL (0.1ms) UPDATE "comments" SET "post_id" = ? WHERE "comments"."id" = ? [["post_id", 23], ["id", 1]]
D, [2015-02-04T09:40:42.635141 #22036] DEBUG -- :  (0.0ms) commit transaction
D, [2015-02-04T09:40:42.638313 #22036] DEBUG -- : Comment Exists (0.1ms) SELECT 1 AS one FROM "comments" WHERE "comments"."post_id" = 1 LIMIT 1
F
Finished in 0.020492s, 48.7997 runs/s, 48.7997 assertions/s.
1) Failure:
BugTest#test_association_stuff [test_foreign_key.rb:46]:
Failed assertion, no message given.
1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.integer :system_id
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments, primary_key: :system_id
end
class Comment < ActiveRecord::Base
belongs_to :post, primary_key: :system_id
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create! system_id: 23
post.comments << Comment.create!
assert Comment.exists?(post: post)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment