我有一个 Users
模型存储在mongodb和a中 UserTypes
模型存储在mysql中。
当我尝试创建新用户时:
Users.create({name: "Shivam", userType: 1})
我得到以下错误:
{ AdapterError: Unexpected error from database adapter: Invalid replacement foreign key value provided for association (`userType`). Cannot interpret `1` as a Mongo id.
(Usually, this is the result of a bug in application logic.)
For more info on Mongo ids, see:
• https://docs.mongodb.com/manual/reference/bson-types/#objectid
• http://sailsjs.com/support
at _afterPotentiallyFinding (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-or-create.js:258:17)
at _afterPotentiallyRunningAfterLC (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-one.js:328:20)
at _maybeRunAfterLC (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-one.js:322:20)
at _afterFetchingRecords (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-one.js:324:13)
at _afterGettingPopulatedPhysicalRecords (/My_Project_Root//node_modules/waterline/lib/waterline/utils/query/help-find.js:754:12)
at /My_Project_Root//node_modules/waterline/lib/waterline/utils/query/help-find.js:151:16
at Object.success (/My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:254:18)
at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1354:30
at _tryToRunCb (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:542:20)
at /My_Project_Root//node_modules/parley/lib/private/Deferred.js:755:16
at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:966:24
at Function.handlerCbs.success (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:826:26)
at Object.fn (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/release-connection.js:77:18)
at wrapper (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3275:19)
at Deferred.parley.now.flaverr.name [as _handleExec] (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1068:17)
at Deferred.exec (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:679:10)
at Deferred.switch (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1306:16)
at /My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:242:92
at /My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:123:18
at Object.success (/My_Project_Root//node_modules/sails-mongo/lib/private/build-std-adapter-method.js:61:47)
at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1354:30
at _tryToRunCb (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:520:18)
cause:
{ AdapterError: Unexpected error from database adapter: Invalid replacement foreign key value provided for association (`userType`). Cannot interpret `1` as a Mongo id.
(Usually, this is the result of a bug in application logic.)
For more info on Mongo ids, see:
• https://docs.mongodb.com/manual/reference/bson-types/#objectid
• http://sailsjs.com/support
at _afterPotentiallyFinding (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-or-create.js:258:17)
at _afterPotentiallyRunningAfterLC (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-one.js:328:20)
at _maybeRunAfterLC (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-one.js:322:20)
at _afterFetchingRecords (/My_Project_Root//node_modules/waterline/lib/waterline/methods/find-one.js:324:13)
at _afterGettingPopulatedPhysicalRecords (/My_Project_Root//node_modules/waterline/lib/waterline/utils/query/help-find.js:754:12)
at /My_Project_Root//node_modules/waterline/lib/waterline/utils/query/help-find.js:151:16
at Object.success (/My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:254:18)
at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1354:30
at _tryToRunCb (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:542:20)
at /My_Project_Root//node_modules/parley/lib/private/Deferred.js:755:16
at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:966:24
at Function.handlerCbs.success (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:826:26)
at Object.fn (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/release-connection.js:77:18)
at wrapper (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3275:19)
at Deferred.parley.now.flaverr.name [as _handleExec] (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1068:17)
at Deferred.exec (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:679:10)
at Deferred.switch (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1306:16)
at /My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:242:92
at /My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:123:18
at Object.success (/My_Project_Root//node_modules/sails-mongo/lib/private/build-std-adapter-method.js:61:47)
at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1354:30
at _tryToRunCb (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:520:18)
name: 'AdapterError',
adapterMethodName: 'create',
modelIdentity: 'users',
raw:
{ Error: Invalid replacement foreign key value provided for association (`userType`). Cannot interpret `1` as a Mongo id.
(Usually, this is the result of a bug in application logic.)
For more info on Mongo ids, see:
• https://docs.mongodb.com/manual/reference/bson-types/#objectid
• http://sailsjs.com/support
at /My_Project_Root//node_modules/sails-mongo/lib/private/machines/private/reify-values-to-set.js:95:35
at /My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3253:15
at baseForOwn (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:2223:14)
at /My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3223:18
at Function.<anonymous> (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3526:13)
at reifyValuesToSet (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/private/reify-values-to-set.js:68:5)
at Object.fn (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/create-record.js:58:7)
at wrapper (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3275:19)
at Deferred.parley.now.flaverr.name [as _handleExec] (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1068:17)
at Deferred.exec (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:679:10)
at Deferred.switch (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1306:16)
at Object.during (/My_Project_Root//node_modules/sails-mongo/lib/private/build-std-adapter-method.js:74:18)
at _makeCallToDuringFn (/My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:109:17)
at /My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:128:7
at Object.success (/My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:91:16)
at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1354:30
at _tryToRunCb (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:542:20)
at /My_Project_Root//node_modules/parley/lib/private/Deferred.js:755:16
at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:966:24
at Function.handlerCbs.success (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:826:26)
at Object.fn (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/get-connection.js:61:18)
at wrapper (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3275:19) code: 'E_CANNOT_INTERPRET_AS_OBJECTID' } },
isOperational: true,
adapterMethodName: 'create',
modelIdentity: 'users',
raw:
{ Error: Invalid replacement foreign key value provided for association (`userType`). Cannot interpret `1` as a Mongo id.
(Usually, this is the result of a bug in application logic.)
For more info on Mongo ids, see:
• https://docs.mongodb.com/manual/reference/bson-types/#objectid
• http://sailsjs.com/support
at /My_Project_Root//node_modules/sails-mongo/lib/private/machines/private/reify-values-to-set.js:95:35
at /My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3253:15
at baseForOwn (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:2223:14)
at /My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3223:18
at Function.<anonymous> (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3526:13)
at reifyValuesToSet (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/private/reify-values-to-set.js:68:5)
at Object.fn (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/create-record.js:58:7)
at wrapper (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3275:19)
at Deferred.parley.now.flaverr.name [as _handleExec] (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1068:17)
at Deferred.exec (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:679:10)
at Deferred.switch (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1306:16)
at Object.during (/My_Project_Root//node_modules/sails-mongo/lib/private/build-std-adapter-method.js:74:18)
at _makeCallToDuringFn (/My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:109:17)
at /My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:128:7
at Object.success (/My_Project_Root//node_modules/sails-mongo/lib/private/do-with-connection.js:91:16)
at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:1354:30
at _tryToRunCb (/My_Project_Root//node_modules/parley/lib/private/Deferred.js:542:20)
at /My_Project_Root//node_modules/parley/lib/private/Deferred.js:755:16
at /My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:966:24
at Function.handlerCbs.success (/My_Project_Root//node_modules/machine/lib/private/help-build-machine.js:826:26)
at Object.fn (/My_Project_Root//node_modules/sails-mongo/lib/private/machines/get-connection.js:61:18)
at wrapper (/My_Project_Root//node_modules/@sailshq/lodash/lib/index.js:3275:19)
code: 'E_CANNOT_INTERPRET_AS_OBJECTID' } }
然而, .populate()
效果很好:
Users.findOne('USER_ID_HERE').populate('userType')
产生以下结果:
{
id: "USER_ID_HERE",
name: "Shivam",
userType: {
id: 1,
label: "Student"
}
}
以下是我的模型:
用户.js:
module.exports = {
datastore: 'mongoDb',
attributes: {
id: {type: 'string', columnName: '_id', required: false},
name: {
type: 'string',
required: false,
allowNull: true,
maxLength: 50,
minLength: 3
},
userType: {model: 'usertypes', required: true},
}
};
和usertypes.js:
module.exports = {
datastore: 'default',
attributes: {
id: {type: 'number', autoIncrement: true},
label: {
type: 'string',
maxLength: 255,
required: true,
unique: true
},
createdAt: false,
updatedAt: false,
users: {collection: 'users', via: 'userType'}
}
};
哪里 default
数据存储是 MySQL
以及 mongoDb
数据存储是 MongoDb
.
以下是版本信息:
Version Info:
: Sails : v1.0.2
: node : v8.1.2
: engine (v8) : 5.8.283.41
: openssl : 1.0.2l
: sails-mongo : ^1.0.1
: sails-mysql : ^1.0.0
: sails-hook-orm : ^2.0.0-16
我做错什么了?
1条答案
按热度按时间ig9co6j11#
对于遇到相同问题的其他人,这里有一个修补程序:
套
dontUseObjectIds: true
适用于所有使用mysql适配器的型号。这似乎欺骗了
sails-mongo
适配器不检查使用sails-mysql
适配器。请注意,这不是一个合适的解决方案,只是一个变通方法。