I'm stumped by a very weird problem and to be honest I'm not even sure what to search for on google.
This is the situation: I am using AdonisJS and it's migration/seeder functionality. I have a seeder that creates data in a table:
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
export default class extends BaseSchema {
protected tableName = 'roll_table_tags'
public async up() {
this.schema.createTable(this.tableName, (table) => {
table.increments('id')
table
.uuid('roll_table_id')
.references('roll_tables.id')
.onDelete('cascade')
.onUpdate('cascade')
table.string('tag_id').references('tags.id').onDelete('cascade').onUpdate('cascade')
table.unique(['roll_table_id', 'tag_id'])
table.index(['roll_table_id', 'tag_id'])
/**
* Uses timestamptz for PostgreSQL and DATETIME2 for MSSQL
*/
table.timestamp('created_at', { useTz: true })
table.timestamp('updated_at', { useTz: true })
})
}
public async down() {
this.schema.dropTable(this.tableName)
}
}
To develop and on production I am using a Postgres Database. For integration tests I am using an SQLite DB.
Now the problem:
If I execute the seeder for the Postgres the id is correctly saved as a UUID field (which is filled in the seeder) If I execute the seeder for the SQLite DB the seeder is trying to save the id as an integer (starting at 1) and I get this error for the subsequent call:
insert into `roll_table_tags` (`created_at`, `roll_table_id`, `tag_id`, `updated_at`) values ('2023-03-24 12:56:59', 1, 'alignment', '2023-03-24 12:56:59') - SQLITE_CONSTRAINT: FOREIGN KEY constraint failed
This is the loop for the seeder
for (const table of data) {
// Tag.query().
await Tag.updateOrCreateMany(
"id",
table.tags.map((tag) => ({ id: tag }))
);
const created = await RollTable.updateOrCreate(
{ name: table.name },
{
id: randomUUID(),
name: table.name,
description: table.description,
}
);
console.log("ID", created.id); // <- This prints a uuid for postgres and an integer for sqlite
await created
.related("tags")
.updateOrCreateMany(
table.tags.map((tag) => ({ rollTableId: created.id, tagId: tag }))
);
}
Why is this happening and how can I prevent it.
EDIT:
In my SQLite editor I can see that the uuid type is transformed to a CHAR(36) type.