在Laravel 4中如何使用BIGINT作为自动增量主键

在Laravel 4中如何使用BIGINT作为自动增量主键

问题描述:

我正在尝试模仿 wordpress的主键大小是BIGINT(20),但是laravel似乎没有本机功能..我看到了页面,并获得了如下代码:

I am trying to mimic wordpress' primary key size which is BIGINT(20) but it seems that laravel doesn't have a native function to do this.. I saw a page in the laravel forums and got a code like this:

$table->bigInteger('id')->primary();

但是当我尝试在artisan migrate期间将外键附加到该ID时,会抛出MYSQL错误:

but when i try to attach a foreign key to that id during artisan migrate, there is a MYSQL error that is thrown:

[异常] SQLSTATE [HY000]:一般错误:1005无法创建表'db.#sql- 1730_15'(errno:150)(SQL:修改表users添加约束users_role_id_foreign外键(role_id)引用roles(id)))(绑定:数组( ))

[Exception] SQLSTATE[HY000]: General error: 1005 Can't create table 'db.#sql- 1730_15' (errno: 150) (SQL: alter table users add constraint users_role_id_foreign foreign key (role_id) references roles (id)) (Bindings: array ( ))

执行此操作的正确方法是什么?或者我在哪里弄错了这件事?

What is the proper way to do this or where do i get this thing wrong?

谢谢!

您很可能也忘记将role_id外键的类型也设置为BIGINT(20).这实际上不是Laravel问题,而是MySQL的问题.

You most likely forgot to also set the type of your role_id foreign key as BIGINT(20) as well. This isn't really a Laravel issue, but rather MySQL's.

顺便说一句,Laravel确实具有本机功能:

By the way, Laravel does have a native function to do this:

$this->bigIncrements('id');

这需要使其成为 unsigned 自动递增主键.