使用knex、bookshelf向sqlite3添加功能
Add function to sqlite3 using knex, bookshelf
我在开发范围内使用 postgresql,它有函数调用 "uuid_generate_v4()"
在测试范围内,我使用 sqlite3,但迁移代码不起作用,因为缺少 "uuid_generate_v4()"。
那我可以重新解决这个问题吗?
connection.schema.createTableIfNotExists('notification', (table) => {
// UUID
table.uuid('id').primary().defaultTo(connection.raw('uuid_generate_v4()'))
// Adds a "created_at" and "updated_at" columns on the database,
// setting these each to "dateTime" types.
table.timestamps()
table.string('type', 255)
table.string('summary', 255)
table.string('text', 255)
table.string('actionText', 255)
table.string('actionUrl', 255)
table.uuid('recipient').references('user.id')
}),
failed with "create table if not exists "notification" ("id" char(36) default uuid_generate_v4(), "created_at" datetime, "updated_at" datetime, "type" varchar(255), "summary" varchar(255), "text" varchar(255), "actionText" varchar(255), "actionUrl" varchar(255), "recipient" char(36), foreign key("recipient") references "user"("id"), primary key ("id")) - SQLITE_ERROR: near "(": syntax error"
正如@mu-is-too-short评论的那样,无论如何我不建议这样做,但这是可以做到的:
let uuidGenerationRaw = connection.client.config.client === 'sqlite3' ?
`(lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' || substr(lower(hex(randomblob(2))),2) || '-' || substr('89ab',abs(random()) % 4 + 1, 1) || substr(lower(hex(randomblob(2))),2) || '-' || lower(hex(randomblob(6))))` :
`uuid_generate_v4()`;
connection.schema.createTableIfNotExists('notification', (table) => {
table.uuid('id').primary().defaultTo(connection.raw(uuidGenerationRaw));
// ... rest of the columns
}),
我在开发范围内使用 postgresql,它有函数调用 "uuid_generate_v4()" 在测试范围内,我使用 sqlite3,但迁移代码不起作用,因为缺少 "uuid_generate_v4()"。 那我可以重新解决这个问题吗?
connection.schema.createTableIfNotExists('notification', (table) => {
// UUID
table.uuid('id').primary().defaultTo(connection.raw('uuid_generate_v4()'))
// Adds a "created_at" and "updated_at" columns on the database,
// setting these each to "dateTime" types.
table.timestamps()
table.string('type', 255)
table.string('summary', 255)
table.string('text', 255)
table.string('actionText', 255)
table.string('actionUrl', 255)
table.uuid('recipient').references('user.id')
}),
failed with "create table if not exists "notification" ("id" char(36) default uuid_generate_v4(), "created_at" datetime, "updated_at" datetime, "type" varchar(255), "summary" varchar(255), "text" varchar(255), "actionText" varchar(255), "actionUrl" varchar(255), "recipient" char(36), foreign key("recipient") references "user"("id"), primary key ("id")) - SQLITE_ERROR: near "(": syntax error"
正如@mu-is-too-short评论的那样,无论如何我不建议这样做,但这是可以做到的:
let uuidGenerationRaw = connection.client.config.client === 'sqlite3' ?
`(lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' || substr(lower(hex(randomblob(2))),2) || '-' || substr('89ab',abs(random()) % 4 + 1, 1) || substr(lower(hex(randomblob(2))),2) || '-' || lower(hex(randomblob(6))))` :
`uuid_generate_v4()`;
connection.schema.createTableIfNotExists('notification', (table) => {
table.uuid('id').primary().defaultTo(connection.raw(uuidGenerationRaw));
// ... rest of the columns
}),