Repository navigation
Closed
Labels
dependencyFor issues and PRs. Things that are related to one or more dependencies.For issues and PRs. Things that are related to one or more dependencies.type: otherDEPRECATED: replace with the "meta" issue typeDEPRECATED: replace with the "meta" issue type
Description
Exception occurs when trying to add two (or more) equal values in a unique field using findOrCreate.
Test code:
var arr = [0, 1];
var User = sequelize.define('User', {
login: {
type: Sequelize.STRING,
unique: true
}
});
User.sync({ force: true })
.then(function () {
sequelize.transaction()
.then(function (tx) {
return Promise.map(arr, function () {
return User.findOrCreate({
where: {
login: 'test'
},
transaction: tx
});
})
.then(function () {
tx.commit();
});
});
});SQL log:
Executing (default): DROP TABLE IF EXISTS "Users" CASCADE;
Executing (default): CREATE TABLE IF NOT EXISTS "Users" ("id" SERIAL , "login" VARCHAR(255) UNIQUE, PRIMARY KEY ("id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND a.attnum = ANY(ix.indkey) AND t.relkind = 'r' and t.relname = 'Users' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (6a28eaa0-9716-4db0-be66-967f8ec12058): START TRANSACTION;
Executing (6a28eaa0-9716-4db0-be66-967f8ec12058): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Executing (6a28eaa0-9716-4db0-be66-967f8ec12058): SET autocommit = 1;
Executing (6a28eaa0-9716-4db0-be66-967f8ec12058): SAVEPOINT "6a28eaa0-9716-4db0-be66-967f8ec12058-savepoint-1";
Executing (6a28eaa0-9716-4db0-be66-967f8ec12058): SAVEPOINT "6a28eaa0-9716-4db0-be66-967f8ec12058-savepoint-2";
Executing (6a28eaa0-9716-4db0-be66-967f8ec12058): SELECT "id", "login" FROM "Users" AS "User" WHERE "User"."login" = 'test' LIMIT 1;
Executing (6a28eaa0-9716-4db0-be66-967f8ec12058): SELECT "id", "login" FROM "Users" AS "User" WHERE "User"."login" = 'test' LIMIT 1;
Executing (6a28eaa0-9716-4db0-be66-967f8ec12058): CREATE OR REPLACE FUNCTION pg_temp.testfunc(OUT response "Users", OUT sequelize_caught_exception text) RETURNS RECORD AS $func_66f952b0ec354cdbb8923433f3248e4a$ BEGIN INSERT INTO "Users" ("id","login") VALUES (DEFAULT,'test') RETURNING * INTO response; EXCEPTION WHEN unique_violation THEN GET STACKED DIAGNOSTICS sequelize_caught_exception = PG_EXCEPTION_DETAIL; END $func_66f952b0ec354cdbb8923433f3248e4a$ LANGUAGE plpgsql; SELECT (testfunc.response).*, testfunc.sequelize_caught_exception FROM pg_temp.testfunc(); DROP FUNCTION IF EXISTS pg_temp.testfunc();
Executing (6a28eaa0-9716-4db0-be66-967f8ec12058): CREATE OR REPLACE FUNCTION pg_temp.testfunc(OUT response "Users", OUT sequelize_caught_exception text) RETURNS RECORD AS $func_2abdfe30c027469dac0b5fe594fb4dda$ BEGIN INSERT INTO "Users" ("id","login") VALUES (DEFAULT,'test') RETURNING * INTO response; EXCEPTION WHEN unique_violation THEN GET STACKED DIAGNOSTICS sequelize_caught_exception = PG_EXCEPTION_DETAIL; END $func_2abdfe30c027469dac0b5fe594fb4dda$ LANGUAGE plpgsql; SELECT (testfunc.response).*, testfunc.sequelize_caught_exception FROM pg_temp.testfunc(); DROP FUNCTION IF EXISTS pg_temp.testfunc();Error stack:
Unhandled rejection Error: Cannot find module 'pg-native'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/home/user/test/node_modules/pg/lib/native/index.js:1:76)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at null.<anonymous> (/home/user/test/node_modules/pg/lib/index.js:78:36)
at /home/user/test/node_modules/lodash/index.js:3159:32
at baseForOwn (/home/user/test/node_modules/lodash/index.js:2089:14)
at baseClone (/home/user/test/node_modules/lodash/index.js:1799:39)
at /home/user/test/node_modules/lodash/index.js:1800:23
From previous event:
at Promise.catch (/home/user/test/node_modules/sequelize/lib/promise.js:21:17)
at Object.<anonymous> (/home/user/test/node_modules/sequelize/lib/model.js:1199:15)
From previous event:
at Promise.then (/home/user/test/node_modules/sequelize/lib/promise.js:21:17)
at module.exports.Model.findOrCreate (/home/user/test/node_modules/sequelize/lib/model.js:1180:8)
at /home/user/test/tests.js:30:16
From previous event:
at /home/user/test/tests.js:29:18
at processImmediate [as _immediateCallback] (timers.js:358:17)
From previous event:
at Promise.then (/home/user/test/node_modules/sequelize/lib/promise.js:21:17)
at Object.<anonymous> (/home/user/test/tests.js:26:2)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3Note: without the transaction is the same error
Reacted by Meotimdihia, 7hibault, Alejandro Corredor and Daniel Bennett
Metadata
Metadata
Assignees
Labels
dependencyFor issues and PRs. Things that are related to one or more dependencies.For issues and PRs. Things that are related to one or more dependencies.type: otherDEPRECATED: replace with the "meta" issue typeDEPRECATED: replace with the "meta" issue type
Activity
tornillo commentedon May 21, 2015
The error occurs when calling optClone->lodash.cloneDeep from findOne and other similar methods.
The PG-module has a property 'native', which is a getter:
During cloning this getter is called and throwing error.
Workarounds:
1: Before Sequelize initialization in application we can remove stupid getter:
It should be documented.
2: Delete getter when require PG in Sequelize connection-manager.js if config.native === false
3: Or we need to patch Units.clone for totally skipping property 'lib' cloning:
cloneDeep: function(obj, fn) { - return lodash.cloneDeep(obj, function (elem) { + return lodash.cloneDeep(obj, function (elem, key) { + // Skip property 'lib' cloning (PG-native error workaround) + if (key === 'lib' && elem.hasOwnProperty('native')) { + return elem; + } +janmeier commentedon May 21, 2015
Hmm, we've seen this problem before - optClone should be handling this problem already by not cloning transaction instances. Which version are you on?
tornillo commentedon May 21, 2015
3.0.1 - latest
optClone clone all properties, pg.native is a getter.
Lodash cloneDeep iterates through all properties and gets a value of each -> getter called.
tornillo commentedon May 21, 2015
...and only then transaction is skipped by checking:
janmeier commentedon May 21, 2015
Hmm, it seems we are trying to clone a sequelize instance - which has a reference to the pg instance... weird..
tornillo commentedon May 21, 2015
Oh, I missed the important thing:
It occurs only when findOne called an the end of findOrCreate
https://github.com/sequelize/sequelize/blob/master/lib/model.js#L1207
zooofeex commentedon May 27, 2015
I am having the same issue
tornillo commentedon May 27, 2015
Commit 4f9b502 solved the problem.
Bug is not reproduced in version 3.1.1
Thank you!
mickhansen commentedon May 27, 2015
Great :)
stoiacs commentedon Jul 17, 2015
In 3.3.2 I had the same issue, workaround by tornillo solved (delete pg.native;).
jbrawnjr commentedon Apr 21, 2016
Still encountering the optClone issue in 3.21.0.
benjaminhon commentedon Jun 1, 2016
i still have this error message occurring, but i have no idea why it came suddenly. At first it didn't have it then suddenly it came. Im using postgres, and its giving me and error that i have no mysql dialect in node modules as well as pg-native
63 remaining items
1valdis commentedon Jul 3, 2020
@superrandres do you use
[email protected]or above?superrandres commentedon Jul 4, 2020
Hello @1valdis thanks for answering. I indicate the versions that I am using:
"pg": "^7.14.0",
"pg-hstore": "^2.3.3",
"sequelize": "^5.22.3"
The MongoDB error has nothing to do with this.
1valdis commentedon Jul 4, 2020
@superrandres you have
[email protected]. Try usingpg@8or above (npm i -s pg@8) and see if the error is still there. I indicated a few post ago that upgradingpgfrom 7 to 8 solved the issue for me.superrandres commentedon Jul 4, 2020
@1valdis This solved the problem, thank you very much.
Webpack: Ignore .native
Uncomment console.log in database
jeromeSH26 commentedon Sep 26, 2020
Hi,
still got this error by just testing the connection (using
native=true) in the connection options. Have to turn it to false.The workaround to delete the pg.native property is failing as this is a readonly property (in typescript)
sshaginyan commentedon Oct 11, 2020
Same issue as @jeromeSH26
soryy708 commentedon Feb 15, 2021
If the failure happens at build time (e.g. when running webpack), and you don't want to use
pg-native, you can add it to webpack'sexternalss.t. it doesn't complain if it can't find the package.ps2goat commentedon Mar 10, 2021
@jeromeSH26 @sshaginyan
This should work in typescript, though I haven't tested.
ps2goat commentedon Mar 10, 2021
I have confirmed that the line above works in Typescript. I had similar issues with TypeORM, not Sequelize. It appears to be an issue with the
pgnpm package, which is odd. I've seen it work on other machines...UPDATE: This fix worked with an older version of Node (10.22.1), but not 14..14.0
papb commentedon Apr 4, 2021
Since version 8, the
.nativegetter is no longer enumerable, therefore this problem can be avoided by upgrading topg8. For this reason I will close this issue now. If anyone still experiences this problem withpg8 please let me know and I will reopen this.However, in principle, it is Sequelize's fault to deep clone these things to begin with. I've opened #13168 to track this.
RahmatAliMalik5 commentedon Jul 31, 2022
I am still experiencing this issue with
pg^8.7.3. I have tried to deletenativeproperty by using thedelete (<any>pg).nativeanddelete pg.native. Also I have setlogging: false. I am usingsequelizeinnestjswithangular ssr. Also usingnative:falseThe details are as follows:Package.json:
I have read all the thread and tried everything commented by the community. Nothing worked for me.