I have a problem with delete using laravel 5.4
I have 3 tables: Users, Posts, Vechicles
User hasMany Posts
Vehicle belognsTo Post
Post hasMany Vehicles
...anyway...when creating the schema for the vehicle table, i use 2 foreign keys:
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->foreign('post_user_id')->references('user_id')->on('posts');
When i want to delete a post, all the vehicles that are related to the post to be deleted....but is not working (it gives an error about constraint)
Can someone tell me what i am wrong? it is that i am using 2 foregin keys?
check this
$table->engine = "InnoDB";
Using one foreign key solve the issue:
Schema::create('vehicles', function (Blueprint $table) {
$table->increments('id');
$table->string('mark');
$table->string('model');
$table->integer('weight');
$table->string('vehicle_state');
$table->integer('post_id')->unsigned();
$table->integer('post_user_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
});
Related
I am using Laravel 7 and PHP 7.4.
I'm working on databases and suddenly stuck over issue when I'm trying to generate a foreign key for user in another table. It should be straight away process and I'm following the doc, still getting error.
General error: 1005 Can't create table carchain_qrcode.sellers (errno: 150 "Foreign key constraint is incorrectly formed
User Table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigInteger('id');
$table->string('name')->unique();
$table->string('email')->unique();
});
}
Sellers Table
public function up()
{
Schema::create('sellers', function (Blueprint $table) {
$table->bigInteger('id');
$table->unsignedBigInteger('user_id');
$table->string('seller_name');
$table->string('seller_email');
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
});
Where have I gone wrong?
I think the problem is that the id of your users table is bigInteger and not bigIncrements or unsignedBigInteger.
In Laravel 7 you can simply do: $table->id() to create the id column.
Short: For quick fix refer to #Aless55 answer. The main thing to keep in mind is that id and foreign_key column types should match.
Correct way:
It is better to use Laravel's built-in APIs for creating primary key and foreign key. More details here:
It will automatically take care of column types, creating indexes, and so on. In addition, sticking to official documentation makes it easy to understand the code by others, and maintain it by the developer (It is just my opinion).
In your case:
Users table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->string('email')->unique();
});
}
Sellers table:
public function up()
{
Schema::create('sellers', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained(); // this will automatically detect which table to reference
$table->string('seller_name');
$table->string('seller_email');
});
}
this error occured due to incorrect data type used on the foreign key which you are going to create. The primary key data type on the users table should be same as the data type which you used in Sellers table as below:
Users Table
$table->id();
Sellers Table
$table->unsignedBigInteger('user_id');
or if you used any other data type on users table primary key then the foreign key in the other table should be same accordingly.
I followed a tutorial about many-to-many relationships with Laravel (7 in my case).
The result is good, I learned a lot, but what I find strange is that I do not have physical relationships between the different tables.
I created a relationship many to many, which should link 3 tables, products, categories and products_categories
My questions :
1- Is it essential to have a physical relationship in the schema of the database?
2- How can I make these relationships appear in my diagram?
Here is a current photo of the database schema :
In this database, I have links between tables :
The Laravel relationships are not the same as your database relationships (MySQL, or others).
You don't need to have a database relationship to have your application working. it is really depending on what you are trying to achieve.
If you want to see the relationships between your tables, make sure to specify the foreign keys in your migration Schema (https://laravel.com/docs/7.x/migrations#foreign-key-constraints) such as:
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
For pivot tables you can also use a migration Schema as follow:
Schema::table('category_product', function (Blueprint $table) {
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('product_id');
$table->foreign('category_id')->references('id')->on('categories');
$table->foreign('product_id')->references('id')->on('products');
});
I have 2 tables in my database. One for Courses and one for Course Chapters.
The migration for the courses looks like this:
Schema::create('courses', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
The migration for the chapters looks like this:
Schema::create('course_chapters', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('course_id');
$table->timestamps();
});
I want the course and the chapter to cascade down, so when i delete a course, the chapter also will be deleted.
Some examples i saw make use of deleting the foreign key but I never signed my column as a foreign key.
For example, normally, I could:
$table->dropForeign('course_id');
$table->foreign('course_id')
->references('id')->on('courses')
->onDelete('cascade');
How can i accomplish this in a (preferably) new migration and on what table should i add the foreign key?
This as it is should go on your course_chapters table:
$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
You don't need to add $table->dropForeign('course_id'); because that will drop the foreign key from the column.
NOTE: and this:
$table->unsignedInteger('course_id');
Should be this:
$table->unsignedBigInteger('course_id');
Because it will throw an error of using different data types.
I have a migration with three tables and i have a question which decide the how the tables will connect to each other and do i really a foreign key on them so what i want to do I have two roles 1 admin - 2 user and i want to assign one of them to each user because i want only admin users to login into backend dashboard as for now i make the relationship between roles and users as one(role)-to-many(users) Also the relationship between posts and users is one(user)-to-many(posts) since the post in only related to one user only but the user can have multiple posts so i need you help here to understand those thing in better way and when i need to setup a Foreign Key in the table.
This is my tables:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('fullname');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
$table->integer('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles');
$table->rememberToken();
$table->timestamps();
});
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->string('content');
$table->string('image');
$table->timestamps();
});
From what I am understanding, you have three tables: roles, users, and posts.
Whenever you have a relationship like a one-to-many, the table corresponding to the many side will contain the foreign key to the table corresponding to the one side. You do not put any foreign key in the other table.
Now the roles and user table share a one-to-many relationship respectively, thus you put the role_id in the user table and do not put any foreign key in the roles table. Similarly, the posts table is on the many side of the relationship (one user can have MANY posts) so you have to put user_id in the posts table but no foreign key in the user table.
After you do create the migrations, to be able to query in between the tables, you need to define model files. These model files will contain methods used to retrieve associated records. For a one-to-many relationship, you will be required to have two models (for both tables) and in those models use functions as: belongsTo for the table with the foreign key, and a hasMany function for the table without the foreign key. For your case, the user model will have a hasMany function for the posts model, and a belongsTo function for the roles model.
Hope that clears it up
I have a situation in Laravel 5.1 where I would like to add a many-to-many relationship to an existing relationship. According to the diagram below, I already have all the items in green working.
The issue is that since there isn't a primary key on the issue_person table, I don't know how to add a many-to-many relationship to Users. Does anyone know how I would go about accomplishing this?
So it appears that a simple answer to this is to write a migration that adds a primary key to the original issue_person pivot table, and then set up a many-to-many relationship between issue_person and user using the position_user table.
My migration looks like this:
public function up()
{
Schema::table('issue_person', function (Blueprint $table) {
$table->increments('id');
});
}
public function down()
{
Schema::table('issue_person', function (Blueprint $table) {
$table->dropColumn('id');
});
}