AdonisJS V4 - belongsToMany - sync()/attach() - invalid input syntax for type uuid: ""

842 views Asked by At

Not sure why Im getting this error: invalid input syntax for type uuid: ""

The beforeCreate hook event works, and inserts the column ID but I still get that weird error. Is there something I'm missing here?

 

Pivot Table:

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()
})

 

Promotion Model:

class Promotion extends Model {
...
  customers() {
    return this
      .belongsToMany('App/Models/Customer')
      .withPivot(['created_at', 'assigned_by', 'id'])
      .pivotModel('App/Models/CustomerPromotion')
  }
...
}

module.exports = Promotion

 

CustomerPromotion Model:

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')
})
...

 

Update: Another weird thing is that it was stored even if I had an error and the UUID is inserted.

See screenshot below:

untitled

1

There are 1 answers

0
frost kazuma On BEST ANSWER

Okay I wasted a few hours in this lmao, nothing wrong with the attach(). I just debugged that one item on the array I'm passing on attach() is just an empty string. hence the error invalid input syntax for type uuid: ""