如何将自动增量设置为非主键? [英] How to set auto increment into non primary key?
本文介绍了如何将自动增量设置为非主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何设置不是自动创建方法主键的数据库自动增量字段?
How would I set a database auto increment field that is not a primary key on the creation method?
唯一的方法是使用原始查询?
The only way is using a raw query?
DB::statement('ALTER TABLE table CHANGE field field INT(10)AUTO_INCREMENT');
推荐答案
It is not implemented to do so. However, I found this over at the Laravel Forums:
Schema::table('table', function(Blueprint $t) {
// Add the Auto-Increment column
$t->increments("some_column");
// Remove the primary key
$t->dropPrimary("table_some_column_primary");
// Set the actual primary key
$t->primary(array("id"));
});
这未经测试,但可以正常工作.我不确定Laravel如何调用其主键,也许您必须先检查一下并改写dropPrimary()
行才能使其正常工作.
This is not tested but should work. I am not sure about how Laravel calls their primary keys, maybe you have to check that first and adapt the dropPrimary()
line in order to make it work.
这篇关于如何将自动增量设置为非主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文