Sequelize in Node/Express - 'no such table: main.User` 错误
Sequelize in Node/Express - 'no such table: main.User` error
我正在尝试使用 Sequelize 构建一个简单的 Node/Express 应用程序,但是当我尝试在我的关系数据库中创建新记录时,出现错误 Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such table: main.User
。基本上,我在 Users
table 中创建一个用户,然后尝试在 Addresses
table 中创建一个相关地址 - 用户创建成功但失败并出现此错误创建地址时...它从哪里获取 table 名称中的 main
前缀? (下方的完整错误读数)...
首先,这是我的程序的概要...
我的 Sequelize 版本是 Sequelize [Node: 6.8.1, CLI: 2.4.0, ORM: 3.29.0]
,我使用 Sequelize CLI 命令 sequelize init
来设置我项目的这一部分。
我正在使用 SQLite3 进行本地开发,在 config/config.json
我将开发数据库定义为
"development": {
"storage": "dev.sqlite",
"dialect": "sqlite"
}
我的用户迁移:
'use strict';
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
first_name: {
type: Sequelize.STRING
},
last_name: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: function(queryInterface, Sequelize) {
return queryInterface.dropTable('Users');
}
};
及地址迁移(略):
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.createTable('Addresses', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
address_line_one: {
type: Sequelize.STRING
},
UserId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: "User",
key: "id"
}
}
})
}
用户模型:
'use strict';
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
models.User.hasOne(models.Address);
}
}
});
return User;
};
地址模型:
'use strict';
module.exports = function(sequelize, DataTypes) {
var Address = sequelize.define('Address', {
address_line_one: DataTypes.STRING,
UserId: DataTypes.INTEGER
}, {
classMethods: {
associate: function(models) {
models.Address.hasOne(models.Geometry);
models.Address.belongsTo(models.User, {
onDelete: "CASCADE",
foreignKey: {
allowNull: false
}
});
}
}
});
return Address;
};
最后,我的路线index.js
:
router.post('/createUser', function(req, res){
var firstName = req.body.first_name;
var lastName = req.body.last_name;
var addressLineOne = req.body.address_line_one;
models.User.create({
'first_name': newUser.firstName,
'last_name': newUser.lastName
}).then(function(user){
return user.createAddress({
'address_line_one': newUser.addressLineOne
})
})
所以当我尝试 post 到 /createUser
时,用户将成功创建并且控制台将显示已创建新地址 (INSERT INTO 'Addresses'...
),但是未创建地址并记录了以下错误:
Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such table: main.User
at Query.formatError (/Users/darrenklein/Desktop/Darren/NYCDA/WDI/projects/world_table/wt_test_app_1/node_modules/sequelize/lib/dialects/sqlite/query.js:348:14)
at afterExecute (/Users/darrenklein/Desktop/Darren/NYCDA/WDI/projects/world_table/wt_test_app_1/node_modules/sequelize/lib/dialects/sqlite/query.js:112:29)
at Statement.errBack (/Users/darrenklein/Desktop/Darren/NYCDA/WDI/projects/world_table/wt_test_app_1/node_modules/sqlite3/lib/sqlite3.js:16:21)
几个月前,我曾用 Sequelize 做过一次这种事情,而且很成功,我终究无法弄清楚我在这里遗漏了什么。为什么应用程序要查找 main.User
,我怎样才能让它查找正确的 table?谢谢!
啊哈!一个小错误使我的整个操作脱轨。迁移文件中,references.model
必须复数!
references: {
model: "Users",
key: "id"
}
繁荣。
我正在尝试使用 Sequelize 构建一个简单的 Node/Express 应用程序,但是当我尝试在我的关系数据库中创建新记录时,出现错误 Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such table: main.User
。基本上,我在 Users
table 中创建一个用户,然后尝试在 Addresses
table 中创建一个相关地址 - 用户创建成功但失败并出现此错误创建地址时...它从哪里获取 table 名称中的 main
前缀? (下方的完整错误读数)...
首先,这是我的程序的概要...
我的 Sequelize 版本是
Sequelize [Node: 6.8.1, CLI: 2.4.0, ORM: 3.29.0]
,我使用 Sequelize CLI 命令sequelize init
来设置我项目的这一部分。我正在使用 SQLite3 进行本地开发,在
config/config.json
我将开发数据库定义为"development": { "storage": "dev.sqlite", "dialect": "sqlite" }
我的用户迁移:
'use strict'; module.exports = { up: function(queryInterface, Sequelize) { return queryInterface.createTable('Users', { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER }, first_name: { type: Sequelize.STRING }, last_name: { type: Sequelize.STRING }, createdAt: { allowNull: false, type: Sequelize.DATE }, updatedAt: { allowNull: false, type: Sequelize.DATE } }); }, down: function(queryInterface, Sequelize) { return queryInterface.dropTable('Users'); } };
及地址迁移(略):
module.exports = { up: function(queryInterface, Sequelize) { return queryInterface.createTable('Addresses', { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER }, address_line_one: { type: Sequelize.STRING }, UserId: { type: Sequelize.INTEGER, allowNull: false, references: { model: "User", key: "id" } } }) }
用户模型:
'use strict'; module.exports = function(sequelize, DataTypes) { var User = sequelize.define('User', { first_name: DataTypes.STRING, last_name: DataTypes.STRING }, { classMethods: { associate: function(models) { models.User.hasOne(models.Address); } } }); return User; };
地址模型:
'use strict'; module.exports = function(sequelize, DataTypes) { var Address = sequelize.define('Address', { address_line_one: DataTypes.STRING, UserId: DataTypes.INTEGER }, { classMethods: { associate: function(models) { models.Address.hasOne(models.Geometry); models.Address.belongsTo(models.User, { onDelete: "CASCADE", foreignKey: { allowNull: false } }); } } }); return Address; };
最后,我的路线
index.js
:router.post('/createUser', function(req, res){ var firstName = req.body.first_name; var lastName = req.body.last_name; var addressLineOne = req.body.address_line_one; models.User.create({ 'first_name': newUser.firstName, 'last_name': newUser.lastName }).then(function(user){ return user.createAddress({ 'address_line_one': newUser.addressLineOne }) })
所以当我尝试 post 到 /createUser
时,用户将成功创建并且控制台将显示已创建新地址 (INSERT INTO 'Addresses'...
),但是未创建地址并记录了以下错误:
Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such table: main.User
at Query.formatError (/Users/darrenklein/Desktop/Darren/NYCDA/WDI/projects/world_table/wt_test_app_1/node_modules/sequelize/lib/dialects/sqlite/query.js:348:14)
at afterExecute (/Users/darrenklein/Desktop/Darren/NYCDA/WDI/projects/world_table/wt_test_app_1/node_modules/sequelize/lib/dialects/sqlite/query.js:112:29)
at Statement.errBack (/Users/darrenklein/Desktop/Darren/NYCDA/WDI/projects/world_table/wt_test_app_1/node_modules/sqlite3/lib/sqlite3.js:16:21)
几个月前,我曾用 Sequelize 做过一次这种事情,而且很成功,我终究无法弄清楚我在这里遗漏了什么。为什么应用程序要查找 main.User
,我怎样才能让它查找正确的 table?谢谢!
啊哈!一个小错误使我的整个操作脱轨。迁移文件中,references.model
必须复数!
references: {
model: "Users",
key: "id"
}
繁荣。