question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

updateOnDuplicate and Postgres 12

See original GitHub issue

Issue Description

What are you doing?

I have the following code, when I run code, it creates all rows as in array without “updating” updateOnDuplicate - is not working for me

const atd=(_data, _table = options.bd.table)=>{
	const sequelize = new Sequelize(options.bd.db, options.bd.login, options.bd.password, options.bd.options);
	let Table = sequelize.define(_table, {
		rating: Sequelize.INTEGER,
		// more lines
		reviewID: Sequelize.STRING
	}, {freezeTableName: true});
	sequelize.sync().then(()=>{
		Table.bulkCreate(_data, {
			updateOnDuplicate: ['reviewID']
		});
	});
};

Tried to use:

updateOnDuplicate: ['reviewID', 'id', 'createdAt', 'updatedAt', 'rating'] // all
updateOnDuplicate: ['reviewID', 'id'] // need and main keys
updateOnDuplicate: ['reviewID', 'rating'] // my keys

What do you expect to happen?

I need to update rows if keys match and if they don’t match - add. Formula - I need: if(inTable.reviewID === _data.reviewID){update_line}else{create_line} Check

What is actually happening?

Creating new items(_data.length) in table

Additional context

https://imgur.com/ZPUqgwR https://imgur.com/a/jVeb9gx

Environment

  • Sequelize version: 5.21.1
  • Node.js version: v12.13.0.
  • Operating System: щinows 10 pro last
  • Postgres: 12.0

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s): postgres
  • I don’t know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don’t know how to start, I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
papbcommented, Oct 23, 2019

You have posted a code snippet that shows the problem but is not entirely self-contained (i.e. I can’t just copy-paste it and run it). Please provide a SSCCE (also known as MCVE/reprex) to allow contributors to work on this on a timely manner. I suggest using the sequelize-sscce repository for this.

1reaction
cjancsarcommented, Oct 23, 2019

@Vadyanga you need to follow the issue template as @sushantdhiman has mentioned.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize bulkCreate updateOnDuplicate for postgresQL?
New sequelize (v5) includes updateOnDuplicate feature for all dialects. Fields to update if row key already exists (on duplicate key ...
Read more >
sequelize query use column name instead model name
Asked Mar 12, 2019 • 1 votes 3 answers. QUESTION ANSWERS ... the updateOnDuplicate option is applied only in the main table and...
Read more >
UPSERT - PostgreSQL wiki
"UPSERT" definition. "UPSERT" is a DBMS feature that allows a DML statement's author to atomically either insert a row, or on the basis...
Read more >
Model - Sequelize
Fields to update if row key already exists (on duplicate key update)? (only supported by MySQL, MariaDB, SQLite >= 3.24.0 & Postgres >=...
Read more >
PostgreSQL Upsert Using INSERT ON CONFLICT statement
This tutorial shows you how to use the PostgreSQL upsert feature to insert or update data if the row that is being inserted...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found