-
-
Save x2764tech/4ff7c015cd5d7137c295 to your computer and use it in GitHub Desktop.
Rails Query by specified primary key issue
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
-- create_table(:posts, {:force=>true}) | |
D, [2015-02-04T09:40:42.602488 #22036] DEBUG -- : [1m[36m (0.2ms)[0m [1mCREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "system_id" integer) [0m | |
-> 0.0041s | |
-- create_table(:comments, {:force=>true}) | |
D, [2015-02-04T09:40:42.602859 #22036] DEBUG -- : [1m[35m (0.1ms)[0m 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 -- : [1m[36m (0.0ms)[0m [1mbegin transaction[0m | |
D, [2015-02-04T09:40:42.625989 #22036] DEBUG -- : [1m[35mSQL (0.1ms)[0m INSERT INTO "posts" ("system_id") VALUES (?) [["system_id", 23]] | |
D, [2015-02-04T09:40:42.626287 #22036] DEBUG -- : [1m[36m (0.0ms)[0m [1mcommit transaction[0m | |
D, [2015-02-04T09:40:42.632388 #22036] DEBUG -- : [1m[35m (0.0ms)[0m begin transaction | |
D, [2015-02-04T09:40:42.632792 #22036] DEBUG -- : [1m[36mSQL (0.0ms)[0m [1mINSERT INTO "comments" DEFAULT VALUES[0m | |
D, [2015-02-04T09:40:42.632980 #22036] DEBUG -- : [1m[35m (0.0ms)[0m commit transaction | |
D, [2015-02-04T09:40:42.633107 #22036] DEBUG -- : [1m[36m (0.0ms)[0m [1mbegin transaction[0m | |
D, [2015-02-04T09:40:42.634920 #22036] DEBUG -- : [1m[35mSQL (0.1ms)[0m UPDATE "comments" SET "post_id" = ? WHERE "comments"."id" = ? [["post_id", 23], ["id", 1]] | |
D, [2015-02-04T09:40:42.635141 #22036] DEBUG -- : [1m[36m (0.0ms)[0m [1mcommit transaction[0m | |
D, [2015-02-04T09:40:42.638313 #22036] DEBUG -- : [1m[35mComment Exists (0.1ms)[0m 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 |
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
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