composite-primary-keys / composite_primary_keys

Composite Primary Keys support for Active Record
1.03k stars 350 forks source link

NoMethodError (undefined method `composite?' for <uuid>:String) #551

Closed LyricL-Gitster closed 3 years ago

LyricL-Gitster commented 3 years ago

Currently running off of master (d815939).

I'm seeing an issue that is apparently related to queries on relations... possibly related to UUID's?

In this example, my users table has a team_id pointing to the teams table. Both are keyed by UUIDs.

1 => User.find_by team: team.id
1 => #<User id: "ee87136f-a71d-448f-86bb-9e975091078f", team_id: "1689d0af-07b5-4410-9f5f-ca6eeafe253e", ...>
2 => User.where(any_condition: value).find_by team: team.id
2 => NoMethodError (undefined method `composite?' for "1689d0af-07b5-4410-9f5f-ca6eeafe253e":String)
3 => User.where(any_condition: value).find_by team_id: team.id
3 => #<User id: "ee87136f-a71d-448f-86bb-9e975091078f", team_id: "1689d0af-07b5-4410-9f5f-ca6eeafe253e", ...>

I'm having trouble getting a full stacktrace, but judging by the method name it seems like an issue with this gem. It's pretty easily worked around by using the right value of team_id: team.id or team: team, but usually Rails is more forgiving and this should probably work.

Thanks for your time!

stevehill1981 commented 3 years ago

@LyricL-Gitster I just ran into this today, and your suggested fix worked beautifully. Thanks so much.

codeodor commented 3 years ago

@LyricL-Gitster could you grab a full stack trace? Thanks!

LyricL-Gitster commented 3 years ago

Sorry, for some reason I wasn't getting a full trace before and I assumed it was just being swallowed. But, now I get this:

irb(main):001:0> User.where team: '123'
/Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/bundler/gems/composite_primary_keys-d81593910833/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb:30:in `ids': undefined method `composite?' for "123":String (NoMethodError)
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/bundler/gems/composite_primary_keys-d81593910833/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb:15:in `queries'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.1/lib/active_record/relation/predicate_builder.rb:105:in `block in expand_from_hash'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.1/lib/active_record/relation/predicate_builder.rb:84:in `each'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.1/lib/active_record/relation/predicate_builder.rb:84:in `flat_map'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.1/lib/active_record/relation/predicate_builder.rb:84:in `expand_from_hash'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.1/lib/active_record/relation/predicate_builder.rb:29:in `build_from_hash'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.1/lib/active_record/relation/query_methods.rb:1091:in `build_where_clause'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.1/lib/active_record/relation/query_methods.rb:645:in `where!'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.1/lib/active_record/relation/query_methods.rb:640:in `where'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.1/lib/active_record/querying.rb:22:in `where'
    from (irb):1:in `<main>'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb/workspace.rb:116:in `eval'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb/workspace.rb:116:in `evaluate'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb/context.rb:450:in `evaluate'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb.rb:567:in `block (2 levels) in eval_input'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb.rb:751:in `signal_status'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb.rb:548:in `block in eval_input'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb/ruby-lex.rb:251:in `block (2 levels) in each_top_level_statement'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb/ruby-lex.rb:233:in `loop'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb/ruby-lex.rb:233:in `block in each_top_level_statement'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb/ruby-lex.rb:232:in `catch'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb/ruby-lex.rb:232:in `each_top_level_statement'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb.rb:547:in `eval_input'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb.rb:481:in `block in run'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb.rb:480:in `catch'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb.rb:480:in `run'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/irb.rb:409:in `start'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.1/lib/rails/commands/console/console_command.rb:70:in `start'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.1/lib/rails/commands/console/console_command.rb:19:in `start'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.1/lib/rails/commands/console/console_command.rb:102:in `perform'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/command.rb:27:in `run'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor.rb:392:in `dispatch'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.1/lib/rails/command/base.rb:69:in `perform'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.1/lib/rails/command.rb:50:in `invoke'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.1/lib/rails/commands.rb:18:in `<main>'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/zeitwerk-2.4.2/lib/zeitwerk/kernel.rb:34:in `require'
    from /Users/lyric/DEV_WORK/thanacare-server/bin/rails:5:in `<main>'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/fork_tracker.rb:10:in `block in fork'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/fork_tracker.rb:10:in `block in fork'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/fork_tracker.rb:8:in `fork'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/fork_tracker.rb:8:in `fork'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/fork_tracker.rb:26:in `fork'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/fork_tracker.rb:8:in `fork'
    from /Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/fork_tracker.rb:26:in `fork'
    from <internal:/Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
    from <internal:/Users/lyric/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
    from -e:1:in `<main>'
codeodor commented 3 years ago

Thanks @LyricL-Gitster!