Skip to content
Advertisement

Knex multiple primary key in a identify relationship where id is an auto increment column

I tried to use every single logic in my mind to resolve this problem, but this is the only solution that it worked.

knex.schema.createTable('documents', (table) => {
      table.integer('id').unsigned().notNullable().unique()
      table.string('path')
      table.enum('type', ['CPF', 'RG'])
      table.integer('user_id').unsigned().notNullable()
      table.foreign('user_id').references('id').inTable('users')
      table.primary(['id', 'user_id'])

      table.timestamps(true, true)
    })
    knex.schema.alterTable(this.tableName, (table) => {
      table.increments('id', { primaryKey: false }).alter()
    })
}

Advertisement

Answer

This topic isn’t a question, this is the way that I found to solve the problem using multiple primary key, where an id is auto_increment in the table.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement