我可以使用sequelize.js
对开发数据库中的表执行INSERT INTO
命令,但不能对测试数据库中的表执行操作。
尽管进行了深入研究,但仍无法解决问题。
一个类似的问题已经发布在这里,尽管我无法用答案来回答我的问题: sequelize with postgres database not working after migration from mysql
这是我相关的迁移文件:
'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');
}
};
这是模型文件:
'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;
};
以下是访问数据库的代码:
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);
});
}
这是通过module.exports
方法导出的:
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);
});
}
我在这里将这种方法用于集成测试(PrimaryTopic表位于同一数据库中,尝试运行它时不会收到任何错误):
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();
});
});
});
我已经在所有代码中搜索了可能的错误,但是还没有发现任何东西。
我可以使用psql
访问测试数据库中的Trees
表,尽管它是空的。
我可以使用相同的代码毫无问题地将值插入开发数据库中的Trees
表中。
这是我尝试运行测试(使用jasmine.js进行测试)时收到的错误:
{ 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 *;' }
答案 0 :(得分:1)
我知道这是旧的,但它可以帮助其他人面临同样的问题。 我从 mysql 迁移到 postgres 并遇到了同样的问题,因为我的表名以大写(“Home”)开头,我试图添加一个以小写开头的表名“home”的约束。
答案 1 :(得分:0)
您需要在数据模型的选项中将FrozenTableName设置为true。否则,sequelize将尝试搜索Trees表
module.exports = (sequelize, DataTypes) => {
var Tree = sequelize.define('Tree', {
title: {
type: DataTypes.STRING,
allowNull: false
},
content: {
type: DataTypes.STRING(10000),
allowNull: false
}
}, {
freezeTableName: true
});
Tree.associate = function(models) {
// associations can be defined here
};
return Tree;
};
有关完整文档:https://sequelize.org/v3/docs/models-definition/#configuration
答案 2 :(得分:0)
如果您使用扩展模型接口,您可以简单地将 tableName: "Trees"
附加到您的模型配置中。
我发现解决的最简单方法是在模型上显式设置 tableName
。正如其他人所提到的,sequelize 默认使用模型的复数形式作为表名。例如 Tree,变成 Trees。
当您查询时,sequelize 会处理与您的模型 Tree
同名的表。通过在模型中定义 tableName
,sequelize 应该搜索正确的表。将 tableName: "Trees"
附加到您的模型配置中,即:
module.exports = (sequelize, DataTypes) => {
class Tree extends Model {
...
}
Tree.init(
{
title: DataTypes.STRING,
...
},
{
sequelize,
modelName: 'Tree',
tableName: 'Trees',
}
);
return Tree;
};