我正在尝试使用Sequelize构建一个简单的Node/Express应用程序,但是当我尝试在我的关系数据库中创建一个新记录时,我收到错误Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such table: main.User
.基本上,我在Users
表中创建一个用户,然后尝试在表中创建一个相关的地址Addresses
- 用户已成功创建但在创建地址时失败并出现此错误...其中是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 }) })
因此,当我尝试发布时/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
,如何才能找到正确的表格?谢谢!
啊哈!一个小错误使我的整个操作脱轨.在迁移文件中,references.model
必须是多元化的!
references: { model: "Users", key: "id" }
繁荣.