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

Add support to use current values in insert Mysql/Postgres #3056

Merged
merged 2 commits into from Nov 29, 2018

Conversation

apellizzn
Copy link
Contributor

@apellizzn apellizzn commented Nov 6, 2018

In Mysql / Postgres add support to update record during INSERT on conflict / duplicate key using current values, implements, fixes #3047

@jsnick
Copy link
Contributor

jsnick commented Nov 6, 2018

I think there is atomic update case like column = column + 1

@apellizzn
Copy link
Contributor Author

apellizzn commented Nov 6, 2018

@jsnick I think we can use column = column + 1 when we want to update the value based on the current value in the database.
What i am doing with this PR is to update the value based on the current record that i am trying to insert.
Using counter = counter + 1 will produce

| id | counter |
|----|---------|
| 1  | 5       |
INSERT INTO foo (id, counter) VALUES (1, 10) ON DUPLICATE KEY UPDATE counter = counter + 1 
| id | counter |
|----|---------|
| 1  | 6       |

Using counter = VALUES(counter) will produce

| id | counter |
|----|---------|
| 1  | 5       |
INSERT INTO foo (id, counter) VALUES (1, 10) ON DUPLICATE KEY UPDATE counter = VALUES(counter)
| id | counter |
|----|---------|
| 1  | 10      |

@marshall007
Copy link

It would be nice if a high-level conflict: 'ignore' | 'replace' | 'error' option were also exposed on EntityManager's save/update/insert APIs.

@apellizzn
Copy link
Contributor Author

@marshall007 Sure, to me this sounds like another PR i would like to merge this one first.

@pleerock
Copy link
Member

QueryBuilder is a complex task since we replace property names in database names and use property names in queries in most of places. In this case looks like we have to use database names, and its confusing.

Lets merge it for now, but someday we'll need to figure out what to do with this confusion.

Thank you for the contribution!

@pleerock
Copy link
Member

ooups looks like a conflict, can you please resolve it?

@apellizzn
Copy link
Contributor Author

@pleerock Sorry for the delay, resolved :)

@@ -532,7 +532,7 @@ export class ColumnMetadata {
* If column is in embedded (or recursive embedded) it extracts its value from there.
*/
getEntityValue(entity: ObjectLiteral, transform: boolean = false): any|undefined {
// if (entity === undefined || entity === null) return undefined; // uncomment if needed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, why its needed?)

Copy link
Contributor Author

@apellizzn apellizzn Nov 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in some cases when you actually insert multiple rows which cause a conflict entity is not defined, so therefore line 595 will break value = entity[this.propertyName];

Database with columns c1, c2, c3 with a unique index on c1 and the following data

| A (c1) | Foo (c2) | Bar (c3) |

If i try to insert the following

| A | Foo2 | Bar2 |
| A | Foo3 | Bar3 |

I just want the database to be | A | Foo3 | Bar3 | i guess this has something to do with the fact that you are actually trying to insert N rows but returning N-1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mm not sure I got it, but okay, lets merge it

@pleerock pleerock merged commit a1ead76 into typeorm:master Nov 29, 2018
@pleerock
Copy link
Member

Thank you!

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

Successfully merging this pull request may close these issues.

Mysql / Postgres on duplicate key update / on conflict use current values
4 participants