用sqlite更改Laravel迁移的主要键
我有以下迁移:
Schema::create('items', function(Blueprint $table) {
$table->uuid('id')->primary();
// more columns ...
});
现在,我们要添加一个额外的自动报重列:
Schema::table('items', function(Blueprint $table) {
$table->dropPrimary('id');
$table->rename('id', 'SystemId')->change();
$table->id();
});
问题:sqlite不允许更改主键
解决方案:建议删除表并使用更改的模式创建表格,
从理论上起作用,但是将代码从我们的第一个迁移到我们的第二次复制,这是干燥的。所以我的问题是:有其他方法可以实现吗?
I have the following migration:
Schema::create('items', function(Blueprint $table) {
$table->uuid('id')->primary();
// more columns ...
});
Now, we want to add an additional auto-increment column:
Schema::table('items', function(Blueprint $table) {
$table->dropPrimary('id');
$table->rename('id', 'SystemId')->change();
$table->id();
});
Problem: SQLite doesn't allow changing the primary key
Solution: It's recommended to delete the table and create it with the changed schema
Of course, that works in theory but it is anything but DRY to copy the code from our first migration to our second. So my question is: Is there another way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,我终于提出了一种通用的解决方案,足以重复使用。将其纳入Laravel会很棒,但是包裹可能更有可能。
以下是 sqlite网站上的指令
So, I finally came up with a solution that is generic enough to be reusable. Would be great to be included into Laravel, but a package is probably more likely.
This follows the instructions on the SQLite website
创建后,您无法以任何重要的方式修改SQLite表。正如您所说,所建议的解决方案是创建一个具有正确要求的新表并将您的数据复制到其中,然后丢弃旧表。那是这样做的唯一方法。
有关此的官方文件: http://sqlite.org/faq.html#q11
You can't modify SQLite tables in any significant way after they have been created. As you said the accepted suggested solution is to create a new table with the correct requirements and copy your data into it, then drop the old table. That's the only way to do this.
Official documentation about this: http://sqlite.org/faq.html#q11