问题描述
不确定我为什么收到此错误: invalid input Syntax for type uuid: ""
beforeCreate
钩子事件有效,并且插入了列ID,但是我仍然收到该奇怪的错误。
这里有我想念的东西吗?
数据透视表:
this.create('customer_promotion',(table) => {
table.uuid('id').primary()
table.uuid('tenant_id')
table.uuid('customer_id')
table.uuid('promotion_id')
table.string('assigned_by')
table.timestamps()
})
促销模式:
class Promotion extends Model {
...
customers() {
return this
.belongsToMany('App/Models/Customer')
.withPivot(['created_at','assigned_by','id'])
.pivotModel('App/Models/CustomerPromotion')
}
...
}
module.exports = Promotion
客户促销模型:
const uuid = use('uuid/v4')
class CustomerPromotion extends Model {
static get table () {
return 'customer_promotion'
}
static boot() {
super.boot()
this.addHook('beforeCreate',async (instance) => {
instance.id = uuid.v4()
})
}
static get incrementing() {
return false
}
}
module.exports = CustomerPromotion
SomeController.js:
...
await promotion.customers().sync(customerIdArray,(row) => {
row.assigned_by = `${first_name} ${last_name}`
row.created_at = moment().format('YYYY-MM-DD')
})
...
更新: 另一个奇怪的事情是,即使我遇到错误并插入了UUID,它也已存储。
请参见下面的屏幕截图:
解决方法
好吧,我在lmao中浪费了几个小时,attach()
没错。
我刚刚调试了attach()
传递的数组中的一项只是一个空字符串。
因此出现错误invalid input syntax for type uuid: ""