Support the ongoing development of Laravel.io →
posted 9 years ago
Database
Last updated 2 years ago.
0

I think increments makes the "id" to be primary. You can drop it with dropPrimary.
You cannot have two primary keys at the same time, you have to drop one and then create the other. Also, every constraint should be an index.

integer('city_id')->unsigned()->index();

Don't make composite primaries, because Eloquent doesn't support models with composite primary keys, use them only for pivot tables (Many-to-Many tables).
Don't create a composite primary key if you have an autoincrement column. That would be a violation against some of the normalization forms (NF2 or NF3, not sure).
Make the city_id a foreign key, and that's it.

Last updated 2 years ago.
0

Firtzberg said:

Also, every constraint should be an index.

integer('city_id')->unsigned()->index();

I don't believe you need the ->index() method because Laravel appears to automatically create an index for foreign keys.

Last updated 2 years ago.
0

For example, this code will generate a table with two indexes. A primary index with field "id" and another index with field "branch_id".

Schema::create('facility', function(Blueprint $table)
{
	$table->increments('id');
	$table->softDeletes();
	$table->string('name', 45);
	$table->integer('branch_id')->unsigned();
	$table->foreign('branch_id')->references('id')->on('branch')->onUpdate('cascade')->onDelete('restrict');
});
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.