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();
but when i try to attach a foreign key to that id during artisan migrate
, there is a MYSQL error that is thrown:
[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
) referencesroles
(id
)) (Bindings: array ( ))
What is the proper way to do this or where do i get this thing wrong?
thanks!
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.
By the way, Laravel does have a native function to do this:
$this->bigIncrements('id');
This takes care of making it unsigned, auto increment and primary key.
When using bigInteger() also applying it to foreign key in some table, make sure you connect it properly 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
});
}
Reference Link of the Laravel 4.2 Doc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With