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

Rails - upsert no unique index found #101

Open
wowremywang opened this issue Dec 24, 2019 · 1 comment
Open

Rails - upsert no unique index found #101

wowremywang opened this issue Dec 24, 2019 · 1 comment

Comments

@wowremywang
Copy link

This is my migration file.

def change
    add_index :book_owners, [:book_id, :book_detail_id], unique: true, where: 'book_id IS NOT NULL and book_detail_id IS NOT NULL'
  end

This is my code.

BookOwners.upsert({
  book_id: 123,
  book_detail_id: 234,
  ...
}, unique_by: { columns: [:book_id, :book_detail_id] })

But I am getting this error.

No unique index found for {:columns=>[:book_id, :book_detail_id]}

@jcquarto
Copy link

I think you just pass the array of fields , not a hash of them. So:
unique_by: [:book_id, :book_detail_id]
which looks for a unique set of book_id and book_detail_id. (no idea what happens in one of those is null)
hope that helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants