Search code examples
mysqlbinaryblobloopbackjsloopback4

Unable to create BLOB/Binary types with LoopBack 4


I'm trying to use Loopback for my new projects, but I've been facing some problems...

I have the habit of storing my UUIDs ID in a binary format at my databases, here's an example:

@model({
  settings: { mysql: { table: 'application' } },
})
export class Application extends Entity {
  @property({
    type: 'buffer',
    required: true,
    generated: false,
    id: true,
    dataLength: 16,
  })
  id: BinaryType;
 [...]
}

But when I try to do the migration, I've been receiving that error from mysql:

"BLOB/TEXT column 'id' used in key specification without a key length"

I really tried everything and nothing works. Hope that you'll be able to help me!

Thanks a lot!


Solution

  • I'll show the answer for this question that I made.

    Just define your model with the following info:

      @property({
        required: true,
        mysql: {
          columnName: 'application_id',
          dataType: 'VARBINARY',
          dataLength: 16,
          nullable: 'NO'
        }
      })
      application_id: string;
    

    It worked like a charm for me :)

    Thank you all!