Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use compound index with association ID #219

Open
CodeMonkeySteve opened this issue Jul 18, 2016 · 2 comments
Open

Can't use compound index with association ID #219

CodeMonkeySteve opened this issue Jul 18, 2016 · 2 comments
Labels
bug Something that was working before, dont anymore.

Comments

@CodeMonkeySteve
Copy link
Contributor

I'm trying to create a compound index that includes a belongs-to association. Here's a short example:

class Source
  include NoBrainer::Document
  has_many :items
end
class Item
  include NoBrainer::Document
  belongs_to :source
  field :gid, required: true, unique: { scope: :source }
  index [:source, :gid]
end
NoBrainer.sync_indexes

source = Source.create!
Item.create!(source: source, gid: 'foo')
Item.where(source: source, gid: 'foo').first

If I specify the index as [:source, :gid], the where call gives a warning that no index is used.

If I specify the index as [:source_id, :gid], it tries to use the index, but fails with:

Array keys can only contain numbers, strings, bools,  pseudotypes, or arrays (got null of type NULL). (RethinkDB::ReqlNonExistenceError)
Backtrace:
r.table("items").get_all([nil, "foo"], {"index" => :source_id_gid}).count
@nviennot
Copy link
Collaborator

This is indeed a bug that needs to be fixed in the scope declaration of the unique constraint.

Meanwhile, you can declare the scope to be source_id instead of source:

  field :gid, required: true, unique: { scope: :source_id }

@CodeMonkeySteve
Copy link
Contributor Author

Setting the scope and the index to use source_id works, thanks.

@zedtux zedtux added the bug Something that was working before, dont anymore. label Feb 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that was working before, dont anymore.
Projects
None yet
Development

No branches or pull requests

3 participants