I trying to create foreign keys for my laravel 5.8 project, but I am getting error:
My migration file
public function up()
{
Schema::table('chatter_discussion', function (Blueprint $table) {
$table->integer('chatter_category_id')->unsigned()->index();
$table->foreign('chatter_category_id')->references('id')->on('chatter_categories')
->onDelete('cascade')
->onUpdate('cascade');
$table->bigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
Schema::table('chatter_post', function (Blueprint $table) {
$table->integer('chatter_discussion_id')->unsigned()->index();
$table->foreign('chatter_discussion_id')->references('id')->on('chatter_discussion')
->onDelete('cascade')
->onUpdate('cascade');
$table->bigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
}
You can just define your relationships within your models and forget about setting foreign keys etc.
https://laravel.com/docs/5.8/eloquent-relationships
So in your ChatterDiscussion/ChatterPost models (which you may have to create) you would have a function like:
public function chatter_category()
{
return $this->hasOne('App\ChatterCategory');
}
And in your ChatterCategory (whihch you may also need to create) you would have the inverse:
public function chatter_discussion()
{
return $this->belongsTo('App\ChatterDiscussion');
}
public function chatter_post()
{
return $this->belongsTo('App\ChatterPost');
}
You can then handle the deletes in each of the delete functions within your models.
Either way, your error is that you are creating the same column name twice. Something like this should work (I have not tested this)
Schema::table('chatter_discussion', function (Blueprint $table) {
$table->foreign('chatter_category_id')->unsigned()->index()->references('id')->on('chatter_categories')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
Schema::table('chatter_post', function (Blueprint $table) {
$table->foreign('chatter_discussion_id')->unsigned()->index()->references('id')->on('chatter_discussion')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
Related
I am trying to make a laravel migration & establishing an foreign key constrain between two tables.
Here is some of my codes:
create_user_education_table
class CreateUserEducationTable extends Migration
{
public function up()
{
Schema::create('user_education', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->onUpdate('CASCADE')
->onDelete('CASCADE');
$table->unsignedBigInteger('institution_id');
$table->foreign('institution_id')
->references('id')
->on('education_institutions')
->onUpdate('CASCADE')
->onDelete('CASCADE');
$table->string('degree')->nullable();
$table->string('grade')->nullable();
$table->year('start_year')->nullable();
$table->year("end_year")->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::table('user_education', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['institution_id']);
});
Schema::dropIfExists('user_education');
}
}
create_educational_institution_table
class CreateEducationInstitutionsTable extends Migration
{
public function up()
{
Schema::create('education_institutions', function (Blueprint $table) {
$table->id();
$table->string('institution_name');
$table->string('country');
$table->string('city');
$table->string('logo')->nullable();
$table->text('description')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('education_institutions');
}
}
while migrating ```user_education_table`` it throwing following error.
What's the point here ?
you must create education_institutions table before of creating user_education table
I have tried different ways to solve this problem, but none of them have solved the problem.This part of the code is for building a relationship in Laravel, but this code works in Laravel 5 but is error-prone in Laravel 7.
Error:
SQLSTATE[HY000]: General error: 1005 Can't create table shoplaravel.role_user (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table role_user add constraint role_user_role_id_foreign foreign key (role_id) references roles (id) on delete cascade on update cascade)
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('title')->nullable();
$table->timestamps();
});
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('title')->nullable();
$table->timestamps();
});
Schema::create('role_user', function (Blueprint $table) {
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('user_id');
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade')
->onUpdate('cascade');
$table->primary(['role_id', 'user_id']);
});
Schema::create('permission_role', function (Blueprint $table) {
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('permission_id');
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('permission_id')
->references('id')
->on('permissions')
->onDelete('cascade')
->onUpdate('cascade');
$table->primary(['permission_id', 'role_id']);
});
Foreign keys need to be of the exact same type as the column they're referencing.
Laravel's default user table migration uses $table->increments('id'); - an alias of $table->bigIncrements('id').
Change $table->increments('id'); to $table->bigIncrements('id'); in your roles and permissions table migrations.
More on migration columns: https://laravel.com/docs/7.x/migrations#creating-columns
Remove BigInteger and use only unsignedInteger
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('title')->nullable();
$table->timestamps();
});
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('title')->nullable();
$table->timestamps();
});
Schema::create('role_user', function (Blueprint $table) {
$table->unsignedInteger('role_id');
$table->unsignedInteger('user_id');
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade')
->onUpdate('cascade');
$table->primary(['role_id', 'user_id']);
});
Schema::create('permission_role', function (Blueprint $table) {
$table->unsignedInteger('role_id');
$table->unsignedInteger('permission_id');
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('permission_id')
->references('id')
->on('permissions')
->onDelete('cascade')
->onUpdate('cascade');
$table->primary(['permission_id', 'role_id']);
});
I was trying to migrate in Laravel with foreign key that points to the auto incremented id of debiturs table:
public function up()
{
Schema::create('debitur', function (Blueprint $table) {
$table->increments('id');
$table->string('nama');
$table->string('email')->unique();
$table->timestamps();
});
}
But when I tried adding a foreign key below, it throws a "150 Foreign Key constraint is incorrectly formed" error.
public function up()
{
Schema::create('tempat_tinggal', function (Blueprint $table) {
$table->increments('id');
$table->integer('id_debitur')->unsigned();
});
Schema::table('tempat_tinggal', function ($table) {
$table
->foreign('id_debitur')
->references('id')
->on('debiturs')
->onDelete('cascade')
->onUpdate('cascade');
});
}
I have tried deleting the database and starting from scratch, but it still throws the same error. I must be missing something...
EDIT: for some reason Laravel does not automatically append 's' to 'debitur' when creating table.
$table
->foreign('id_debitur')
->references('id')
->on('debitur') <-------
->onDelete('cascade')
->onUpdate('cascade');
Your code is looking fine may be you have a some data in your table which does not reffering to the reffered table.
public function up()
{
Schema::create('tempat_tinggal', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('id_debitur');
});
Schema::table('tempat_tinggal', function ($table) {
$table
->foreign('id_debitur')
->references('id')
->on('debiturs')
->onDelete('cascade')
->onUpdate('cascade');
});
}
How would you go to create a relationship between the folowing tables:
Users table:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('firstname');
$table->string('surename');
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('country')->nullable();
$table->string('postcode')->nullable();
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Setting_type table:
Schema::create('setting_types', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Settings table:
Schema::create('settings', function (Blueprint $table) {
$table->increments('id');
$table->string('value');
$table->timestamps();
});
Setting_type_user table:
Schema::create('setting_type_user', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('type_id')->unsigned();
$table->integer('setting_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('type_id')->references('id')->on('setting_types');
$table->foreign('setting_id')->references('id')->on('settings');
$table->timestamps();
});
This is the result I want to get:
{"id":1,"value":"578943205.jpg","created_at":"2017-07-18 00:00:00","updated_at":null,"pivot":{"setting_id":1,"user_id":1,"type_id":1}}
It depends on many things, but it looks like you want to use many-to-many relationship here.
First, in settings pibot table change it to:
$table->unsignedInteger('user_id');
$table->unsignedInteger('type_id');
It's also a good idea to add foreign key constraints to the table:
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('type_id')->references('id')->on('setting_types');
Then define belongsToMany() relationship in both User and SettingType models. Since you don't follow naming conventions, you have to define foreign keys manually. In the User model:
public function settingTypes()
{
return $this->belongsToMany('App\SettingType', 'settings', 'user_id', 'type_id');
}
And in the SettingType model:
public function users()
{
return $this->belongsToMany('App\User', 'settings', 'type_id', 'user_id');
}
I'm trying to create a ploymorphic many-to-many relationship that also includes an additional relationship. I can't seem to figure out how to get Eloquent to map the additional field to a model.
I have projects, users on those projects, and project roles that dictate permissions for that user on the project.
My tables:
Schema::create('projects', function(Blueprint $table){
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->string('name');
$table->integer('possessor_id');
$table->string('possessor_type');
});
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->string('email');
$table->string('name');
$table->string('username');
});
Schema::create('project_accessors', function(Blueprint $table){
$table->increments('id');
$table->timestamps();
$table->integer('project_id')->unsigned();
$table->foreign('project_id')->references('id')->on('projects');
$table->integer('project_role_id')->unsigned();
$table->foreign('project_role_id')->references('id')->on('project_roles');
$table->integer('entity_id')->unsigned();
$table->string('entity_type');
});
Schema::create('project_roles', function(Blueprint $table){
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->string('name');
$table->string('permissions');
$table->integer('project_id')->unsigned();
$table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
});
I define projects on users like this:
public function projects()
{
return $this->morphToMany('App\Project', 'entity', 'project_accessors')->withTimestamps()->withPivot('project_role_id');
}
I define users and roles on projects like this:
public function roles()
{
return $this->hasMany('App\ProjectRole');
}
public function users()
{
return $this->morphedByMany('App\User', 'entity', 'project_accessors')->withTimestamps()->withPivot('project_role_id');
}
Is there a way to map project_role_id to an actual instance of my Project Role model?