我在節點v6.9.5上的Express v4.15.2服務器中使用了Bookshelf v0.10.3和Knex v0.12.9。如何在事務中插入後保存一對多關係
我有一個user
表與一個關聯的identity
表,由identity_user
表加入。該表採用Knex創建:
Promise.all([
knex.schema.createTable('user', table => {
table.increments('id');
table.string('username');
table.string('password');
table.unique('username');
}),
knex.schema.createTable('identity', table => {
table.increments('id');
table.string('type');
table.string('value');
}),
knex.schema.createTable('identity_user', table => {
table.integer('user_id').references('user.id');
table.integer('identity_id').references('identity.id');
table.unique(['user_id', 'identity_id']);
})
]);
我的書架模式是這樣的:
bookshelf.plugin([ 'registry', 'bookshelf-camelcase' ]);
const Identity = bookshelf.model('Identity', {
tableName: 'identity',
user() {
return this.belongsTo('User', 'identity_user');
}
});
const User = bookshelf.model('User', {
tableName: 'user',
initialize() {
this.on('saving', model => {
if (model.hasChanged('username')) {
return model.set('username', String(model.attributes.username).toLowerCase());
}
});
this.on('saving', model => {
if (model.hasChanged('password')) {
return bcrypt.hash(model.attributes.password, 10)
.then(hash => model.set('password', hash));
}
});
},
identities() {
return this.hasMany('Identity', 'identity_user');
}
});
在我createUser
功能,我想添加一個新用戶,並在一個事務中的身份。
function createUser(req, res, next) {
const { email, username, password } = req.body;
// some data validation
return bookshelf.transaction((t) => {
return new User({ username, password }).save(null, { transacting: t })
.then(user => new Identity({ type: 'email', value: email }).save({ userId: user.attributes.id }, { transacting: t }))
.then(() => res.sendStatus(204))
.catch(err => {
// handle PostgreSQL unique violation error
});
});
}
當我運行的服務器,並嘗試註冊一個新用戶,我收到以下錯誤:
insert into "identity" ("type", "user_id", "value") values ($1, $2, $3) returning "id" - column "user_id" of relation "identity" does not exist { error: insert into "identity" ("type", "user_id", "value") values ($1, $2, $3) returning "id" - column "user_id" of relation "identity" does not exist
這是PostgreSQL的錯誤(代碼42703 - 未定義列),但一切似乎正確設置。我真的可以用另一雙眼睛來看這個。我錯過了什麼?
在此先感謝!
感謝您的回覆。我試過這個,但得到:「TypeError:user.identities(...)。attach不是一個函數」。更令人困惑的是,用戶和身份都被創建,儘管關係不被添加到鏈接表。我是否必須手動回滾捕獲中的更改? –
對不起,我忽略了模型定義中的兩個錯誤。 – flaviodesousa
如果事務範圍內沒有捕獲到異常'bookshelf.transaction(t => {<這裏是範圍>}),那麼事務會自動回滾。但是如果它被抓到(而不是重新拋出),那麼提交會自動被書架觸發, – flaviodesousa