我在开发范围内使用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')
}),
失败,"如果不存在,则创建表 "通知"("id" 字符(36) 默认值 uuid_generate_v4(), "created_at" 日期时间, "updated_at" 日期时间, "类型" 瓦尔查尔(255), "摘要" 瓦尔查尔(255), "文本" 瓦尔查尔(255), "动作文本" 瓦尔查尔(255), "操作网址" 瓦尔查尔(255), "收件人" 字符(36), 外键("收件人") 引用 "用户"("id"), 主键 ("id")) - SQLITE_ERROR:靠近"(":语法错误"
正如@mu太短的评论,无论如何我不建议这样做,但这就是可以做到的:
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
}),