SailsJS 1.0:Mongo 中模型的 .create() 错误,单向关联到存储在 MySQL 中的模型
Posted
技术标签:
【中文标题】SailsJS 1.0:Mongo 中模型的 .create() 错误,单向关联到存储在 MySQL 中的模型【英文标题】:SailsJS 1.0 : Error in .create() for a model in Mongo with One Way Association to a model stored in MySQL 【发布时间】:2019-01-13 05:16:58 【问题描述】:我有一个 Users
模型存储在 MongoDb 和一个 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"
这是我的模型:
Users.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
我做错了什么?
【问题讨论】:
我还报告了一个Bug:github.com/balderdashy/sails/issues/4484 【参考方案1】:对于遇到相同问题的其他人,这里有一个修补程序:
使用 MySQL 适配器为所有模型设置 dontUseObjectIds: true
。
这似乎欺骗了 sails-mongo
适配器,使其不使用 sails-mysql
适配器检查模型外键的 ObjectId。
请注意,这不是一个正确的解决方案,而只是一种解决方法。
【讨论】:
以上是关于SailsJS 1.0:Mongo 中模型的 .create() 错误,单向关联到存储在 MySQL 中的模型的主要内容,如果未能解决你的问题,请参考以下文章