I am able to use sequelize.js
to do an INSERT INTO
command for a table in my development database, but not in my test database.
Despite researching thoroughly, I have not been able to resolve the issue.
A similar question has been posted here, though I have not been able to answer my question with the answers: sequelize with postgres database not working after migration from mysql
Here is my relevant migration file:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Trees', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
title: {
type: Sequelize.STRING,
allowNull: false
},
content: {
type: Sequelize.STRING(10000),
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Trees');
}
};
Here is the model file:
'use strict';
module.exports = (sequelize, DataTypes) => {
var Tree = sequelize.define('Tree', {
title: {
type: DataTypes.STRING,
allowNull: false
},
content: {
type: DataTypes.STRING(10000),
allowNull: false
}
}, {});
Tree.associate = function(models) {
// associations can be defined here
};
return Tree;
};
Here is the code that accesses the database:
const setTree = (treeVal, callback) => {
console.log(`this would be the part`);
Tree.create({
title: 'Tree',
content: JSON.stringify(treeVal)
})
.then((treeStr) => {
let primaryTopics = JSON.parse(treeStr.content);
callback(null, primaryTopics);
})
.catch((err) => {
callback(err);
});
}
This is exported in the module.exports
method:
callTree(callback) {
return Tree.findOne({
where: {
title: 'Tree'
}
})
.then((treeStr) => {
if (treeStr === null) {
return callback(`not defined yet`);
}
let primaryTopics = treeStr.content;
primaryTopics = JSON.parse(primaryTopics);
callback(null, primaryTopics);
})
.catch((err) => {
callback(err);
});
}
And I'm pulling this method for an integration test here (the PrimaryTopic table is in the same database, and I receive no errors trying to run it):
beforeEach((done) => {
this.primaryTopic;
sequelize.sync({force: true}).then((res) => {
PrimaryTopic.create({
title: 'Title: Hello World',
content: '<p>Content: Hello World</p>'
})
.then((primaryTopic) => {
this.primaryTopic = primaryTopic;
treeQueries.buildTree((err, res) => {
if (err) {
console.error(err);
}
});
done();
})
.catch((err) => {
console.log(err);
done();
});
});
});
I've searched through all the code for possible errors, but haven't found anything yet.
I can use psql
to access the Trees
table in the test database, though it is empty.
I can use the same code to insert a value into the Trees
table in the development database with no issues.
Here is the error I receive when I try to run a test (using jasmine.js for testing):
{ SequelizeDatabaseError: relation "Trees" does not exist
at Query.formatError (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/sequelize/lib/dialects/postgres/query.js:363:16)
at query.catch.err (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/sequelize/lib/dialects/postgres/query.js:86:18)
at tryCatcher (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/bluebird/js/release/promise.js:689:18)
at Async._drainQueue (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues [as _onImmediate] (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:756:18)
at tryOnImmediate (timers.js:717:5)
at processImmediate [as _immediateCallback] (timers.js:697:5)
name: 'SequelizeDatabaseError',
parent:
{ error: relation "Trees" does not exist
at Connection.parseE (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/pg/lib/connection.js:553:11)
at Connection.parseMessage (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/pg/lib/connection.js:378:19)
at Socket.<anonymous> (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/pg/lib/connection.js:119:22)
at Socket.emit (events.js:160:13)
at addChunk (_stream_readable.js:269:12)
at readableAddChunk (_stream_readable.js:256:11)
at Socket.Readable.push (_stream_readable.js:213:10)
at TCP.onread (net.js:599:20)
name: 'error',
length: 104,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: '13',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_relation.c',
line: '1160',
routine: 'parserOpenTable',
sql: 'INSERT INTO "Trees" ("id","title","content","createdAt","updatedAt") VALUES (DEFAULT,\'Tree\',\'[{"title":"Title: Hello World","id":1,"secondaryTopics":[]}]\',\'2018-08-14 06:38:37.243 +00:00\',\'2018-08-14 06:38:37.243 +00:00\') RETURNING *;' },
original:
{ error: relation "Trees" does not exist
at Connection.parseE (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/pg/lib/connection.js:553:11)
at Connection.parseMessage (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/pg/lib/connection.js:378:19)
at Socket.<anonymous> (/home/siddhartha/Documents/01-Studio/01-Commercial-Public/01-Komodo/2018-resources/node_modules/pg/lib/connection.js:119:22)
at Socket.emit (events.js:160:13)
at addChunk (_stream_readable.js:269:12)
at readableAddChunk (_stream_readable.js:256:11)
at Socket.Readable.push (_stream_readable.js:213:10)
at TCP.onread (net.js:599:20)
name: 'error',
length: 104,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: '13',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_relation.c',
line: '1160',
routine: 'parserOpenTable',
sql: 'INSERT INTO "Trees" ("id","title","content","createdAt","updatedAt") VALUES (DEFAULT,\'Tree\',\'[{"title":"Title: Hello World","id":1,"secondaryTopics":[]}]\',\'2018-08-14 06:38:37.243 +00:00\',\'2018-08-14 06:38:37.243 +00:00\') RETURNING *;' },
sql: 'INSERT INTO "Trees" ("id","title","content","createdAt","updatedAt") VALUES (DEFAULT,\'Tree\',\'[{"title":"Title: Hello World","id":1,"secondaryTopics":[]}]\',\'2018-08-14 06:38:37.243 +00:00\',\'2018-08-14 06:38:37.243 +00:00\') RETURNING *;' }
Here's a link to the full repository.
I know this is old but it could help somebody else facing same issue. I migrated from mysql to postgres and got same issue because the name of my table started with upper case ("Home") and I was trying to add a constraint with table name "home" which starts with lowercase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With