2016-10-10 29 views
0

欣賞您可以提供的任何幫助。bookshelf.js交易不與附加工作

我正在嘗試創建'Runway'模型,然後在事務內爲多對多關係添加'模型'ID。

在附加期間發生錯誤時(即事務未回滾),仍然創建該模型。我究竟做錯了什麼?

export function create(object) { 
    var data = _.pick(object, ['name', 'userId']); 

    return Bookshelf.transaction((t) => { 
    return Runway.forge(data) 
     .save(null, {transaction: t}) 
     .then((runway) => { 
      return runway.models().attach(object.models, {transaction: t}); 
     }) 
    }) 
    .catch(err => { 
    winston.error('Error creating runway', {err: err}); 
    throw err; 
    }); 
} 

日誌:

Mon, 10 Oct 2016 06:07:59 GMT knex:tx trx1: Starting top level transaction 
{ method: 'insert', 
    options: {}, 
    timeout: false, 
    cancelOnTimeout: false, 
    bindings: 
    [ 2016-10-10T06:07:59.769Z, 
    'bf649bbf-c37f-426a-98e3-707e49eb17c6', 
    'my custom runway', 
    2016-10-10T06:07:59.769Z, 
    '66da84f6-45f4-4217-a995-dfb92cb246f1' ], 
    __knexQueryUid: 'c54606e4-7e4c-4a04-b66b-2f747df1ad6d', 
    sql: 'insert into `runways` (`created_at`, `id`, `name`, `updated_at`, `userId`) values (?, ?, ?, ?, ?)' } 
{ method: 'insert', 
    options: {}, 
    timeout: false, 
    cancelOnTimeout: false, 
    bindings: 
    [ 'f0a3bc21-2315-4b83-9cac-bab1fc019f1555', 
    'bf649bbf-c37f-426a-98e3-707e49eb17c6' ], 
    __knexQueryUid: '6125efce-3ef4-4327-a4ed-445634a26057', 
    sql: 'insert into `runways_models` (`modelId`, `runwayId`) values (?, ?)' } 
Mon, 10 Oct 2016 06:07:59 GMT knex:tx trx1: releasing connection 
2016-10-10T06:07:59.815Z ERROR [winston-fh] Error creating runway [METADATA]: {"err":{"message":"insert into `runways_models` (`modelId`, `runwayId`) values ('f0a3bc21-2315-4b83-9cac-bab1fc019f1555', 'bf649bbf-c37f-426a-98e3-707e49eb17c6') - ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`figurehappy`.`runways_models`, CONSTRAINT `runways_models_modelid_foreign` FOREIGN KEY (`modelId`) REFERENCES `users` (`id`))","stack":"Error: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`figurehappy`.`runways_models`, CONSTRAINT `runways_models_modelid_foreign` FOREIGN KEY (`modelId`) REFERENCES `users` (`id`))\n at Query.Sequence._packetToError (/Users/Nim/Workspace/figurehappy/node_modules/mysql/lib/protocol/sequences/Sequence.js:51:14)\n at Query.ErrorPacket (/Users/Nim/Workspace/figurehappy/node_modules/mysql/lib/protocol/sequences/Query.js:83:18)\n at Protocol._parsePacket (/Users/Nim/Workspace/figurehappy/node_modules/mysql/lib/protocol/Protocol.js:280:23)\n at Parser.write (/Users/Nim/Workspace/figurehappy/node_modules/mysql/lib/protocol/Parser.js:74:12)\n at Protocol.write (/Users/Nim/Workspace/figurehappy/node_modules/mysql/lib/protocol/Protocol.js:39:16)\n at Socket.<anonymous> (/Users/Nim/Workspace/figurehappy/node_modules/mysql/lib/Connection.js:109:28)\n at emitOne (events.js:96:13)\n at Socket.emit (events.js:188:7)\n at readableAddChunk (_stream_readable.js:177:18)\n at Socket.Readable.push (_stream_readable.js:135:10)\n at TCP.onread (net.js:542:20)\n --------------------\n at Protocol._enqueue (/Users/Nim/Workspace/figurehappy/node_modules/mysql/lib/protocol/Protocol.js:141:48)\n at Connection.query (/Users/Nim/Workspace/figurehappy/node_modules/mysql/lib/Connection.js:214:25)\n at /Users/Nim/Workspace/figurehappy/node_modules/knex/lib/dialects/mysql/index.js:124:18\n at Promise._execute (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/debuggability.js:284:9)\n at Promise._resolveFromExecutor (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/promise.js:480:18)\n at new Promise (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/promise.js:77:14)\n at Client._query (/Users/Nim/Workspace/figurehappy/node_modules/knex/lib/dialects/mysql/index.js:118:12)\n at Client.query (/Users/Nim/Workspace/figurehappy/node_modules/knex/lib/client.js:187:24)\n at Runner.<anonymous> (/Users/Nim/Workspace/figurehappy/node_modules/knex/lib/runner.js:129:36)\n at Runner.tryCatcher (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/util.js:16:23)\n at Runner.query (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/method.js:15:34)\n at /Users/Nim/Workspace/figurehappy/node_modules/knex/lib/runner.js:55:21\n at tryCatcher (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/util.js:16:23)\n at /Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/using.js:185:26\n at tryCatcher (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/util.js:16:23)\n at Promise._settlePromiseFromHandler (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/promise.js:509:31)\n at Promise._settlePromise (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/promise.js:566:18)\n at Promise._settlePromise0 (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/promise.js:611:10)\n at Promise._settlePromises (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/promise.js:690:18)\n at Promise._fulfill (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/promise.js:635:18)\n at PromiseArray._resolve (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/promise_array.js:125:19)\n at PromiseArray._promiseFulfilled (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/promise_array.js:143:14)\n at Promise._settlePromise (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/promise.js:571:26)\n at Promise._settlePromise0 (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/promise.js:611:10)\n at Promise._settlePromises (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/promise.js:690:18)\n at Async._drainQueue (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/async.js:138:16)\n at Async._drainQueues (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/async.js:148:10)\n at Immediate.Async.drainQueues (/Users/Nim/Workspace/figurehappy/node_modules/bluebird/js/release/async.js:17:14)\n at runCallback (timers.js:570:20)\n at tryOnImmediate (timers.js:550:5)\n at processImmediate [as _immediateCallback] (timers.js:529:5)","code":"ER_NO_REFERENCED_ROW_2","errno":1452,"sqlState":"23000","index":0}} 

insert into `runways_models` (`modelId`, `runwayId`) values ('f0a3bc21-2315-4b83-9cac-bab1fc019f1555', 'bf649bbf-c37f-426a-98e3-707e49eb17c6') - ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`figurehappy`.`runways_models`, CONSTRAINT `runways_models_modelid_foreign` FOREIGN KEY (`modelId`) REFERENCES `users` (`id`)) 
Error: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`figurehappy`.`runways_models`, CONSTRAINT `runways_models_modelid_foreign` FOREIGN KEY (`modelId`) REFERENCES `users` (`id`)) 

回答

0

對不起......這是一個用戶錯誤。

我應該寫{transacting:t},而不是{transaction:t}。