This might be a fairly obvious race condition - apologies in advance if that's the case.
I've got a simple database schema, and a small set of tests running against it; between every test, I'm using sync
to clear the database:
beforeEach(function(done) {
return sequelize.sync({ force: true }).then(function() { done(); });
});
My User
model only has a few columns:
const ROLES = Object.freeze({
admin: 'ADMIN',
user: 'USER'
});
...
sequelize.define('User', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
email: {
type: DataTypes.STRING,
unique: true
},
role: {
type: DataTypes.ENUM,
values: _.values(ROLES),
defaultValue: ROLES.user
},
passwordBcrypt: DataTypes.STRING
})
And running a simple query against it:
User.findOne({ where: { email: '[email protected]' } });
On a fairly regular basis (once in every 5-10 runs), this test and others like it are failing with either:
Unhandled rejection SequelizeDatabaseError: cache lookup failed for index 43264
at [object Object].module.exports.Query.formatError (/snip/node_modules/sequelize/lib/dialects/postgres/query.js:421:16)
at [object Object].<anonymous> (/snip/node_modules/sequelize/lib/dialects/postgres/query.js:105:21)
or:
SequelizeDatabaseError: cache lookup failed for type 43089
at [object Object].module.exports.Query.formatError (/snip/node_modules/sequelize/lib/dialects/postgres/query.js:421:16)
at [object Object].<anonymous> (/snip/node_modules/sequelize/lib/dialects/postgres/query.js:105:21)
— neither of which mean much to me, but appear to be related to the enum
type based on some light searching.
Here are some sample full query logs, both for the success case (1) and failure cases (2, 3):
1: https://gist.githubusercontent.com/dylanpyle/468ae01f86cbec9eaec6/raw/2d270d3fad599c5f102f80bb624bf307f7182397/gistfile1.txt
2: https://gist.githubusercontent.com/dylanpyle/0930cbddb99982a245f2/raw/6e4bde8da06dea6d59858210668c10019a5a447b/gistfile1.txt
3: https://gist.githubusercontent.com/dylanpyle/b38168fd69a6cf4ad19c/raw/bfb0d2501d82b1b46c159af57118ec9dca122abf/gistfile1.txt
Running Sequelize 2.1.0, iojs 1.7.1, PostgreSQL 9.3.5 on x86_64-apple-darwin14.0.0
A couple more notes, after trying a few other approaches:
sync
call to run after each test (afterEach
in mocha) rather than before, solves the problem - but isn't desirable due to the possibility of bad test data remaining in the db if a previous run failed.If you can create a small, self-contained test-case I can try to run it a couple of times and see if I get the same error. But without some actual code we can run its hard to debug further.
I'm pretty sure its a race condition though..
Update for anyone who might stumble across this in future: _finally_ figured out what was going on here. I had a stray sequelize.sync()
in a different part of my test setup... and I wasn't waiting for it to resolve, so it was running in parallel with other code.
Most helpful comment
Update for anyone who might stumble across this in future: _finally_ figured out what was going on here. I had a stray
sequelize.sync()
in a different part of my test setup... and I wasn't waiting for it to resolve, so it was running in parallel with other code.