neo4jrb / activegraph

An active model wrapper for the Neo4j Graph Database for Ruby.
http://neo4jrb.io
MIT License
1.4k stars 276 forks source link

Relational query in wrong order #1611

Closed shu-bc closed 4 years ago

shu-bc commented 4 years ago

Suppose a book model has_many pages like this

class Book
  has_many :out, :pages, type: :has, model_class: :Page
end

book.page_ids can return an array of unique ids of related pages instances book.page_ids = [....]; book.save is supposed to update its relation, as it does in v9.x.

But in v10.0, this doesn't work, since it will issue delete queries that delete existing relation only AFTER issuing create queries. This causes trouble if some id in the array is included in the existing relations. In an extreme case, if you pass the exact same id array, then this will completely destroy existing relations.

This can be solved by just reversing the order of relational queries.

Additional information which could be helpful if relevant to your issue:

Code example (inline, gist, or repo)

Runtime information:

Neo4j database version: neo4j 3.4.0 neo4j gem version: v10.0 neo4j-core gem version:

klobuczek commented 4 years ago

@shu-bc it would be always helpful to have a complete spec. Are you saying that this spec will fail?

it 'does not delete pages on reassignment` do
  page = Page.create
  book = Book.create(pages: page)
  book.page_ids = [page.id]
  book.save
  expect(book.pages).to be_present
end
amitsuryavanshi commented 4 years ago

Confirming that, this is a issue. If you have a non integer id_property you get this issue. Thanks for reporting @shu-bc, we are on it.

shu-bc commented 4 years ago

@klobuczek yes, exactly.

@amitsuryavanshi oh yes, that is the case. Our id is created by SecureRandom.uuid