php artisan migrate foreign key error - php

Master table POll
public function up()
{
Schema::create('poll', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('poll_question', 255);
$table->boolean('status',1)->default(0)->index();
$table->unsignedInteger('order');
});
}
Detail table
public function up()
{
Schema::create('poll_option', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('poll_id');
$table->string('poll_option', 255);
$table->unsignedInteger('vote_poll_option');
$table->unsignedInteger('order');
$table->boolean('status',1)->default(0)->index();
$table->foreign('poll_id')->references('id')->on('poll')->onUpdate('cascade')->onDelete('cascade');
});
}
When I run php artisan with foreign key
SQLSTATE[HY000]: General error: 1005 Can't create table mydb.#sql-6f4_433 (errno: 150 "Foreign key constraint is incorrectly formed")
Note: I m using laravel 5.2 and mysql type already Innodb What is main causes to incorrectly formed

in Details table
public function up()
{
Schema::create('poll_option', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('poll_id')->unsigned(); //Change Here
$table->string('poll_option', 255);
$table->unsignedInteger('vote_poll_option');
$table->unsignedInteger('order');
$table->boolean('status',1)->default(0)->index();
$table->foreign('poll_id')
->references('id')->on('poll')
->onDelete('CASCADE')
->onUpdate('CASCADE');
});
}
this will work for you

Related

Laravel: How to solve "SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint"

I am making migrations where there is a particular migration that maps one table with various other tables using a foreign key. and I am getting the following error while running the migration.
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table bookmaps add constraint bookmaps_subject_id_foreign foreign key (subject_id) references subject (id) on delete cascade)
Here is the migration that is creating the error:
public function up()
{
Schema::create('bookmaps', function (Blueprint $table) {
$table->unsignedBigInteger('book_id')->unique();
$table->unsignedBigInteger('subject_id')->nullable();
$table->unsignedBigInteger('grade_id')->nullable();
$table->unsignedBigInteger('author_id')->nullable();
$table->unsignedBigInteger('catagory_id')->nullable();
$table->unsignedBigInteger('language_id')->nullable();
$table->timestamps();
});
Schema::table('bookmaps', function($table) {
$table->foreign('book_id')->references('id')->on('book')->onDelete('cascade');
$table->foreign('subject_id')->references('id')->on('subject')->onDelete('cascade');
$table->foreign('grade_id')->references('id')->on('grade')->onDelete('cascade');
$table->foreign('author_id')->references('id')->on('author')->onDelete('cascade');
$table->foreign('catagory_id')->references('id')->on('catagories')->onDelete('cascade');
$table->foreign('language_id')->references('id')->on('language')->onDelete('cascade');
});
}
Migrations for other related tables are:
public function up()
{
Schema::create('book', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('discription')->nullable();
$table->string('book_file')->nullable();
$table->string('card_image')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->timestamps();
});
Schema::table('book', function($table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
The table created by above migration is only mapped by foreign key rest of the below migrations are not mapped.
public function up()
{
Schema::create('subject', function (Blueprint $table) {
$table->id();
$table->string('subject_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('grade', function (Blueprint $table) {
$table->id();
$table->string('grade_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('author', function (Blueprint $table) {
$table->id();
$table->string('author_name');
$table->string('author_discription')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('grade', function (Blueprint $table) {
$table->id();
$table->string('grade_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('author', function (Blueprint $table) {
$table->id();
$table->string('author_name');
$table->string('author_discription')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('catagories', function (Blueprint $table) {
$table->id();
$table->string('catagories_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('language', function (Blueprint $table) {
$table->id();
$table->string('language_name');
$table->timestamps();
});
}
What could be the problem?
You have to create the subject migration before the bookmarks. When Laravel tries to migrate the bookmarks table, there is a foreign key linked to the subject table that it does not find.

Laravel 7 migration :Syntax error or access violation: 1068 Multiple primary key defined

when I try to do php artisan migrate it gives me this error
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table users add primary key users_user_id_primary(user_id))
I m pretty new to laravel tho
here is my migrations :
User migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id('user_id')->primary();
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
});
}
Post migration
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id('post_id');
$table->timestamps();
$table->string('content');
$table->foreignId('user_id')->constrained();
$table->primary(['post_id', 'user_id']);
});
}
Categories migration
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id('id_cat')->primary();
$table->string('nom');
});
}
Marques migration
public function up()
{
Schema::create('marques', function (Blueprint $table) {
$table->id('marque_id');
$table->foreignId('cat_id')->constrained();
$table->string('designation');
$table->primary(['marque_id','cat_id']);
});
}
UserMarques migration
public function up()
{
Schema::create('user_marques', function (Blueprint $table) {
$table->foreignId('cat_id')->constrained();
$table->foreignId('marque_id')->constrained();
$table->timestamps();
$table->primary(['marque_id','user_id']);
});
}
Use $table->increments('user_id'); instead of $table->id('user_id')->primary();
and with others.
BTW, i prefer only id instead of with model name, so in this case i prefer this:
$table->increments('id');
Update for Laravel 7.x
Laravel 7.x migration comes with id method
public function id($column = 'id')
{
return $this->bigIncrements($column);
}
so in this case: you can use id like this
$table->id();
PS: increments key comes with primary key

How to pass foreign key laravel

I'm trying to pass the foreign key but am stuck with this error Base table or view already exists: 1050 Table 'favorites' already exists"
how can I pass them all the foreign keys and the normal id like $table->bigInteger('user_id')->unsigned();
tables
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->Increments('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('product_id')->unsigned();
$table->timestamps();
});
Schema::create('favorites', function (Blueprint $table){
$table->bigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products');
});
}
public function down()
{
Schema::dropIfExists('favorites');
}
You're migrating the table twice, pass the foreign keys in the same migration
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('favorites');
}
Hope this helps
Other answers are correct. If ever you've missed the needed tasks before doing it, kindly try this one
1) Remove or delete the table you've migrated
2) In migrations table, delete the record which consists the name "favorites"
3) Copy and paste thi code inside the "up" function with this one
Schema::create('favorites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('product_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('product_id')->references('id')->on('products');
});
4) Run this command again
php artisan migrate

can not add foreign key

I wrote some migration but the foreign key's will not be added and give me the low error.
I dont see everything is suspicious.
General error : 1251 cannot add foreign key constraint (SQL:alter table 'advertising' add constraint 'advertising_category_id_foreign' foreign key ('category_id')refrences 'category' ('id') on delete restrict on update cascade)
I change id type in related migration's,but it didn't work.
I turned the id in related migration's to index(),but agane didn't work.
public function up()
{
Schema::create('advertising', function (Blueprint $table){
$table->increments('id')->index();
$table->integer('user_id')->unsigned()->index();
$table->string('ranking')->nullable();
$table->string('status')->nullable();
$table->string('name')->nullable();
$table->string('family')->nullable();
$table->string('advertising_name')->nullable();
$table->string('phone')->nullable();
$table->string('email')->nullable();
$table->string('address')->nullable();
$table->string('country-city')->nullable();
$table->integer('category_id')->unsigned()->nullable();
$table->string('week_days')->nullable();
$table->string('time_open')->nullable();
$table->string('time_close')->nullable();
$table->string('body')->nullable();
$table->string('facebook')->nullable();
$table->string('twitter')->nullable();
$table->string('instagram')->nullable();
$table->integer('service_garanti_id')->unsigned()->nullable();
$table->integer('professional_id')->unsigned()->nullable();
$table->integer('insurance_Limit_id')->unsigned()->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')->onUpdate('cascade');
$table->foreign('category_id')->references('id')->on('category')
->onDelete('restrict')->onUpdate('cascade');
$table->foreign('service_garanti_id')->references('id')
->on('service_garanti')
->onDelete('restrict')->onUpdate('cascade');
$table->foreign('professional_id')->references('id')
->on('professional')->onDelete('restrict')->onUpdate('cascade');
$table->foreign('insurance_Limit_id')->references('id')
->on('insurance_limit')
->onDelete('restrict')->onUpdate('cascade');
});
The related migration's :
public function up()
{
Schema::create('category', function (Blueprint $table){
$table->increments('id');
$table->string('body')->nullable();
$table->timestamps();
});
public function up()
{
Schema::create('service_garanti', function (Blueprint $table){
$table->increments('id')->index();
$table->string('body')->nullable();
$table->timestamps();
});
public function up()
{
Schema::create('Professional', function (Blueprint $table){
$table->increments('id')->index;
$table->string('body')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('Insurance_Limit', function (Blueprint $table){
$table->increments('id')->index();
$table->string('body')->nullable();
$table->timestamps();
});
}
Try changing the order of your migrations so that the tables containing the foreign values are created first (category, service_garanti, etc.) and then create the advertising table and make the key connections. The tables and columns must already exist in order for the migration to succeed.
The other option if you know the keys are correct and will be created, it to just disable the key checking at runtime by adding this to the top of your migration.
public function up(){
Schema::disableForeignKeyConstraints();
Schema::create('advertising', function (Blueprint $table){
...
}
}

multiple foreign key that is also primary key in schema builder( laravel )

I have this schema builder (I omitted the class extension and the schema down part)
public function up()
{
Schema::create('cat', function(Blueprint $table)
{
$table->bigIncrements('id');
$table->timestamps();
$table->string('path_img');
$table->softDeletes();
});
}
this:
public function up()
{
Schema::create('campo', function(Blueprint $table)
{
$table->bigIncrements('id');
$table->timestamps();
$table->string('nome');
});
}
and this:
public function up()
{
Schema::create('campo_cat', function(Blueprint $table)
{
$table->timestamps();
$table->bigInteger('id_campo')->unsigned();
$table->bigInteger('id_cat')->unsigned();
$table->primary(array('id_campo','id_cat'));
$table->foreign(array('id_campo','id_cat'))->references(array('id','id'))->on(array('campo','cat'));
});
}
What I am trying to do is to state that the table 'campo_cat' has 2 entries that are BOTH primary key and are also foreign key, referenced to cat id and campo id.
1)If I use unsigned when I migrate I get an array to string conversion error exception
2)Is unsigned needed? or I can just define:
$table->unsigned('..');
as both 1) and 2) campo_cat doesn't create
The problem seems to be related to:
$table->foreign(array('id_campo','id_cat'))
->references(array('id','id'))
->on(array('campo','cat'));
EDIT:
I managed to make it work by doing:
public function up()
{
Schema::create('campo_cat', function(Blueprint $table)
{
$table->timestamps();
$table->bigInteger('id_campo')->unsigned();
$table->bigInteger('id_cat')->unsigned();
$table->primary(array('id_campo','id_cat'));
$table->foreign('id_campo')->references('id')->on('campo');
$table->foreign('id_cat')->references('id')->on('cat');
});
}
AND add
->unsigned(); //to the primary key in the other 2 scheme
Still wonder why if i use array it throws an error.

Categories