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

Multiple Insert + OnConflict + Ignore result in json parsing issues. #2597

Open
stevenolay opened this issue Feb 5, 2024 · 0 comments
Open

Comments

@stevenolay
Copy link

stevenolay commented Feb 5, 2024

Line of code in question: https://github.com/Vincit/objection.js/blob/main/lib/queryBuilder/operations/InsertOperation.js#L57

Description

Suppose you have the following query.

     const recordsToInsert = [... an array of records where a subset of the records clash with a unique index.]

      await this._model
        .query()
        .insert(recordsToInsert)
        .onConflict(['user_id', 'deduplication_key'])
        .ignore()

Let's say that the length of recordsToInsert is 10 and 3 out the 10 records violate the uniqueness constraint.

https://github.com/Vincit/objection.js/blob/main/lib/queryBuilder/operations/InsertOperation.js#L57

Referring to this line ^ the length of this.models is 10 but the length of ret is 7. This is because 3 of the records are dropped from the result set due to .ignore. After the loop iterates 7 times it passes an undefined value into this.models[i].$setDatabaseJson(ret[i]); causing unexpected errors when accessing properties of undefined.

This edge case is only encountered when inserting records with .onConflict.ignore that hit a uniqueness constraint issue causing the ret(the result set) to be less than the length of the original array passed to .insert.

This does not occur when using .onConflict.merge because the length of the result set remains the same.

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

1 participant