在Laravel 4中如何使用BIGINT作为自动增量主键 [英] How do you use a BIGINT as an Auto-Incrementing Primary Key in Laravel 4

查看:282
本文介绍了在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 自动递增主键.

这篇关于在Laravel 4中如何使用BIGINT作为自动增量主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆