2

I have this table:

BookUnit
-------------------
id PK
unit PK
sequence PK
book_id FK

So, i can have records with data:

id: 1
unit: 1
sequence: 1

id: 2
unit: 1
sequence: 2

but i can't have more than one repetead:

id: 3
unit: 1
sequence: 2

So, i need to create a composed primary key with id,sequence and unit.

I try this way:

class BookUnitSchema extends Schema {
  up () {
    this.create('book_unit', (table) => {
      table.increments()
      table.integer('book_id').references('id').inTable('books').notNullable()
      table.integer('unit').notNullable().primary()
      table.integer('sequence').notNullable().primary()
      table.string('description')
      table.integer('qt_question')
      table.boolean('status').defaultTo(false)
      table.integer('user_id').references('id').inTable('users')
      table.timestamps()
    })
  }

  down () {
    this.drop('book_unit')
  }
}

But i'm getting:

error: multiple primary keys for table "book_unit" are not allowed

I don't find in docs example with more than one primary key...

bla
  • 995
  • 3
  • 11
  • 44

2 Answers2

5

you also use primary

table.primary(['unit','sequence'])

it's work for me
More information visit knex.js

Amit Kadivar
  • 798
  • 4
  • 12
2

It's simple, i fixed using:

table.unique(['unit', 'sequence'])
bla
  • 995
  • 3
  • 11
  • 44