Closed kazu728 closed 2 years ago
Kysely just builds the SQL, and doesn't try to create the same API for all dialects. If the foreign keys need to be defined on the table level, call addForeignKeyConstraint
for the table builder instead of using the column builder:
await db.schema
.createTable('pet')
.addColumn('id', 'integer', (col) => col.increments().primaryKey())
.addColumn('name', 'varchar', (col) => col.notNull().unique())
.addColumn('owner_id', 'integer')
.addColumn('species', 'varchar')
.addForeignKeyConstraint(
'owner_id_foreign_key',
['owner_id'],
'person',
['id'],
(constraint) => constraint.onDelete('cascade')
)
.execute()
I added some documentation about this.
I'm not sure that the following migration sample code doesn't generate correct SQL about reference foreign key on MySQL. https://github.com/koskimas/kysely#migrations
Environment
I changed the column type from integer to bigint for MySQL.
The generated sql
One of expected SQL