How do you use BIGINT as an auto-incrementing primary key in Laravel 4

I am trying to mimic the size of a wordpress primary key, which is BIGINT (20), but it seems that laravel has no built-in function for this ... I saw a page on the laravel forums and got code similar to this:

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

but when I try to connect a foreign key to this identifier during artisan migrate to a new version, a MYSQL error occurs:

[Exception] SQLSTATE [HY000]: general error: 1005 Unable to create table 'db. # sql- 1730_15 '(error number: 150) (SQL: change users table, add restriction users_role_id_foreign foreign key ( role_id ) refers to roles ( id )) (Bindings: array ())

How to do it right or where can I go wrong?

Thanks!

+5
source share
2 answers

You most likely forgot to set the role_id foreign key type as BIGINT (20). This is not a Laravel problem, but MySQL.


By the way, Laravel has a built-in function:

 $this->bigIncrements('id'); 

This will help make unsigned , auto-increment and primary key .

+16
source

When using bigInteger () while also applying it to a foreign key in some table, make sure you connect it correctly with unsignedBigInteger (),

 public function up() { Schema::create('create_this_table_after_users', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedBigInteger('user_id'); // Other Columns }); Schema::table('create_this_table_after_users', function($table) { $table->foreign('user_id')->references('id')->on('users'); // Other Constraints }); } 
+2
source

Source: https://habr.com/ru/post/1493878/


All Articles