here is the issue. I am banging my head for hours now on this. Laravel is not allowing me to have a primary key (it says multiple but you can see it has only one). And further more as there is issue causing in primary key in teachers table. I am not able to use this as a foreign key in courses table.
Migration file for teacher table
class CreateTeachersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('teachers', function (Blueprint $table) {
$table->increments('teacher_id')->primary();
$table->string('email', 200);
$table->string('first_name',300)->nullable();
$table->string('last_name',300)->nullable();
$table->string('account_status',50)->default('inactive');
$table->string('subscription_status',50);
$table->string('account_reset_code',400)->nullable();
$table->string('passkey',500);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('teachers');
}
}
Migration File for courses table
class CreateCoursesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('courses', function (Blueprint $table) {
$table->increments('course_id')->primary();
$table->integer('teacher_id_fr');
$table->string('course_name', 500);
$table->string('course_code',300);
$table->string('subject_area',300);
$table->string('course_level',100);;
$table->string('grade_periods',100);
$table->timestamps();
});
Schema::table('courses',function(Blueprint $table){
$table->foreign('teacher_id_fr')->references('teacher_id')->on('teachers')->onDelete('cascade')->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('courses');
}
}
Issue raised on migrating
Syntax Error or access violation. 1068 Multiple primary keys found
.
What I've tried by now
1) Making the primary column unsigned using unsigned method.
2) using InnnoDB engine type for table.
3) And much other things as suggested for similiar posts on the forums but nothing seems to work for me.
EDIT
I've removed the primary() method from both files at $table->increments('teacher_id'). As this is said Laravel marks auto increments columns as primary keys.
But now the error is on courses table as it says.
General error : 1215 cannot add foreign key constraint
You can define Multiple primanry key in migration file as below:
$table->primary(array('field_name1', 'field_name2'));
Try to replace this, remove primary(), increments automatically creates it primary
$table->increments('teacher_id')->primary();
to
$table->increments('teacher_id');
For new error try this General error : 1215 cannot add foreign key constraint
public function up()
{
Schema::create('courses', function (Blueprint $table) {
$table->increments('course_id');
$table->integer('teacher_id_fr');
$table->string('course_name', 500);
$table->string('course_code',300);
$table->string('subject_area',300);
$table->string('course_level',100);;
$table->string('grade_periods',100);
$table->timestamps();
$table->foreign('teacher_id_fr')
->references('teacher_id')->on('teachers')
->onDelete('cascade')
->onUpdate('cascade');
});
}
I was able to fix the issue with a minute change. I modified courses migration file like below and it worked like a charm
$table -> integer('teacher_id_fr') -> unsigned();
Reason
Actually both the referenced and referencing columns (teacher.teacher_id and course.teacher_id_fr in this case) must be of same type.
Having said that, Laravel marks autoincrement columns as "Primary" and of type unsigned. So make sure to change foregin key column to match the type of primary key i.e unsigned. which the above modification does.
Hope it clarifies the issue and solution. If you have anything else on your mind. Please give it a shot here.
Happy Learning!
Related
I have already checked the other topics but no solution worked with me. I tried the last time:
public function up()
{
if(Schema::hasColumn('orders_products_variations','id')) {
Schema::table('orders_products_variations',function (Blueprint $table) {
$table->unsignedInteger('id')->change();
$table->dropPrimary();
$table->dropColumn('id');
});
}
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('clients', function (Blueprint $table) {
$table->string('id');
});
}
And the error message is :
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key (SQL: alter table orders_products_variations drop primary key)
Any idea?
A migration class contains two methods: up and down. The up method is
used to add new tables, columns, or indexes to your database, while
the down method should reverse the operations performed by the up
method.
Dropping Columns
To drop a column, use the dropColumn on the Schema builder inside down method
if (Schema::hasColumn('table_name', 'column_name')) {
Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn('column_name');
});
}
Adding / Dropping Columns
PS When you have to add a new column to the existing table in Laravel, you must
create new migration .
php artisan make:migration add_column_to_table
I’m developing a Laravel project and I created the models, when I run the migrations on my computer (where I’m developing) everything works fine, even the relations between tables. The problem comes when I upload the code to the production server and when I run the migrations where I create the relations, I got an error.
In the code below I leave some commented lines, those lines are things that I tried to do to solve the problem, but nothing worked.
I tried to set the table engine to innoDB on both tables, but it didn’t work.
I tried to set the id datatype as increments on both tables, and the column deviceTypeID that is going to serve as foreign key, I defined it as unsigned and integer and it didn’t work.
I even checked the PHP, Composer, Doctrine/dbal and mysql versions on both computers and are the same (or almosr the same).
Developing Computer:
PHP: 7.3
Composer: 1.8.6
MySQL Server: 5.7.23
Doctrine/dbal: 2.9
Production Server:
PHP: 7.3
Composer: 1.9
MySQL Server: 5.7.25
Doctrine/dbal: 2.9
(It runs Ubuntu Server 18.04)
I also tried to run the project on another computer an see if I got the same error, but it didn't come, everything worked fine.
I also ran this query "alter table devices add constraint devices_devicetypeid_foreign foreign key (deviceTypeID) references devicestype(id) on delete cascade" without all the quotes directly on mysql command line and it worked. So the problem is not related to the user privileges.
It always crash on production when it tries to run the code on AddRelationDevicesDeviceType
class CreateDevicesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('devices', function (Blueprint $table) {
//$table->engine = 'innoDB'
$table->bigIncrements('id');
//$table->increments('id');
$table->unsignedBigInteger('deviceTypeID');
//$table->integer('deviceTypeID')->unsigned();
$table->timestamps();
});
}
}
class CreateDevicesTypeTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('devicesType', function (Blueprint $table) {
//$table->engine = 'innoDB'
$table->bigIncrements('id');
//$table->increments('id');
$table->string("Device Type");
$table->text("Description");
$table->timestamps();
});
}
}
class AddRelationDevicesDeviceType extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('devices', function($table) {
$table->foreign('deviceTypeID')
->references('id')->on('devicesType')
->onDelete('cascade');
});
}
}
This is the error:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `devices` add constraint `devices_devicetypeid_foreign` foreign key (`deviceTypeID`) references `devicestype`(`id`) on delete cascade) at /var/www/html/MYPROJECT/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the databa se's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 Doctrine\DBAL\Driver\PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint") /var/www/html/MYPROJECT/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:119
2 PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint") /var/www/html/MYPROJECT/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:117
I I figured out how to solve my problem.
The problem was when I ran the first migration where I create the table “devicesType” MySQL changed the table name to lower case, so it ended up being “devicestype” and when I tried to refer that table in the last migration where I create the relation Laravel couldn’t find the table.
My development server wasn’t case sensitive while my production server was case sensituve.
I also change the "deviceTypeID" from "unsignedBigInteger" to "bigInteger" and I specify the the type unsigned using the method "unsigned"
This is my new code if is useful to someone:
class CreateDevicesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('devices', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('deviceTypeID')->unsigned();
$table->timestamps();
});
}
}
class CreateDevicesTypeTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('devicestype', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("Device Type");
$table->text("Description");
$table->timestamps();
});
}
}
class AddRelationDevicesDeviceType extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('devices', function($table) {
$table->foreign('deviceTypeID')
->references('id')->on('devicestype')
->onDelete('cascade');
});
}
}
My Laravel migration keeps getting error when reset/refresh:
ORA-01758: table must be empty to add mandatory (NOT NULL)
The migration is as follow:
public function up()
{
Schema::table('kasbank', function (Blueprint $table) {
$table->dropColumn('name_bn');
$table->dropColumn('id_rekon');
$table->string('name_kb')->after('kode');
$table->integer('id_rek')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('kasbank', function (Blueprint $table) {
$table->dropColumn('id_rek');
$table->dropColumn('name_kb');
$table->integer('id_rekon')->nullable();
$table->string('name_bn')->after('kode');
});
}
FYI, column id_rekon is initially nullable in the database.
What's missed from my migration?
The problem is that your table is not empty and the current rows will be given NULL as a default value, but the column is not allowed to be NULL.
You should be able to get around it by setting a default value.
From the Oracle docs:
However, a column with a NOT NULL constraint can be added to an
existing table if you give a default value; otherwise, an exception is
thrown when the ALTER TABLE statement is executed.
Try:
$table->integer('id_rek')->default($value)->nullable();
From: https://laravel.com/docs/5.5/migrations
Framework: Laravel 5.2
Database: MySQL
PHP: 7.0
I have the table "pops":
Schema::create('pops', function (Blueprint $table) {
$table->string('id')->primary()->index();
$table->string('ab_test_parent_id')->nullable();
$table->string('cloned_parent_id')->nullable();
$table->timestamps();
});
And the table "conversions":
Schema::create('conversions', function (Blueprint $table) {
$table->string('id')->primary()->index();
$table->integer('users')->default(null);
$table->string('name',256)->default(null);
$table->string('pop_id');
$table->foreign('pop_id')->references('id')->on('pops')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
I have set a foreign key (pop_id) in the "conversions" table. Now does it's mean that the foreign key (pop_id) is also a index? if not... what i need to do in order to make it a index?
Thank?
You can just do this.
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddIndexToLeads extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('leads', function(Blueprint $table)
{
$table->index('trader_id');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('leads', function (Blueprint $table)
{
$table->dropIndex(['trader_id']);
});
}
}
credit to #bobbybouwmann
Here is a perfect solution for you.
Up
$table->string('poptin_id')->index('poptin_id');
Down
$table->dropIndex(['poptin_id']);
Suggestion to fix your migrations
$table->string('id')->primary()->index();
replace above with below
$table->increments('id');
and fix your foreign key as below
$table->integer('poptin_id')->index('poptin_id')->unsigned();
I was facing problem when i just write :-
$table->index('column_name');
So to be more exact first you need to create column with desired column type then assign it to be index like this :-
$table->string('column_name');
$table->index('column_name');
//or
$table->string('column_name')->index();
I hope this helps
Laravel only adds a foreign key constraint and doesn't add index implicitly. But some databases such as MySQL automatically index foreign key columns.
If you need to add index to a field in another migration. Then you could do
$table->index('email');
I've created a table using migration like this:
public function up()
{
Schema::create('despatch_discrepancies', function($table) {
$table->increments('id')->unsigned();
$table->integer('pick_id')->unsigned();
$table->foreign('pick_id')->references('id')->on('picks');
$table->integer('pick_detail_id')->unsigned();
$table->foreign('pick_detail_id')->references('id')->on('pick_details');
$table->integer('original_qty')->unsigned();
$table->integer('shipped_qty')->unsigned();
});
}
public function down()
{
Schema::drop('despatch_discrepancies');
}
I need to change this table and drop the foreign key reference & column pick_detail_id and add a new varchar column called sku after pick_id column.
So, I've created another migration, which looks like this:
public function up()
{
Schema::table('despatch_discrepancies', function($table)
{
$table->dropForeign('pick_detail_id');
$table->dropColumn('pick_detail_id');
$table->string('sku', 20)->after('pick_id');
});
}
public function down()
{
Schema::table('despatch_discrepancies', function($table)
{
$table->integer('pick_detail_id')->unsigned();
$table->foreign('pick_detail_id')->references('id')->on('pick_details');
$table->dropColumn('sku');
});
}
When I run this migration, I get the following error:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1025 Error on rename of
'./dev_iwms_reboot/despatch_discrepancies' to
'./dev_iwms_reboot/#sql2-67c-17c464' (errno: 152) (SQL: alter table
despatch_discrepancies drop foreign key pick_detail_id)
[PDOException]
SQLSTATE[HY000]: General error: 1025 Error on rename of
'./dev_iwms_reboot/despatch_discrepancies' to
'./dev_iwms_reboot/#sql2-67c-17c464' (errno: 152)
When I try to reverse this migration by running php artisan migrate:rollback command, I get a Rolled back message, but it's not actually doing anything in the database.
Any idea what might be wrong? How do you drop a column that has a foreign key reference?
You can use this:
Schema::table('despatch_discrepancies', function (Blueprint $table) {
$table->dropForeign(['pick_detail_id']);
$table->dropColumn('pick_detail_id');
});
If you take a peak at dropForeign source, it will build the foreign key index name for you if you pass the column name as an array.
It turns out; when you create a foreign key like this:
$table->integer('pick_detail_id')->unsigned();
$table->foreign('pick_detail_id')->references('id')->on('pick_details');
Laravel uniquely names the foreign key reference like this:
<table_name>_<foreign_table_name>_<column_name>_foreign
despatch_discrepancies_pick_detail_id_foreign (in my case)
Therefore, when you want to drop a column with foreign key reference, you have to do it like this:
$table->dropForeign('despatch_discrepancies_pick_detail_id_foreign');
$table->dropColumn('pick_detail_id');
Update:
Laravel 4.2+ introduces a new naming convention:
<table_name>_<column_name>_foreign
Update:
Larave > 8.x introduces a new function
dropConstrainedForeignId('pick_detail_id');
This will delete the column as well as the foreign key of the column
I had multiple foreign keys in my table and then I had to remove foreign key constraints one by one by passing column name as index of the array in down method:
public function up()
{
Schema::table('offices', function (Blueprint $table) {
$table->unsignedInteger('country_id')->nullable();
$table->foreign('country_id')
->references('id')
->on('countries')
->onDelete('cascade');
$table->unsignedInteger('stateprovince_id')->nullable();
$table->foreign('stateprovince_id')
->references('id')
->on('stateprovince')
->onDelete('cascade');
$table->unsignedInteger('city_id')->nullable();
$table->foreign('city_id')
->references('id')
->on('cities')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('offices', function (Blueprint $table) {
$table->dropForeign(['country_id']);
$table->dropForeign(['stateprovince_id']);
$table->dropForeign(['city_id']);
$table->dropColumn(['country_id','stateprovince_id','city_id']);
});
}
Using below statement does not work
$table->dropForeign(['country_id','stateprovince_id','city_id']);
Because dropForeign does not consider them seperate columns that we want to remove. So we have to drop them one by one.
The key (for me) to solving this was to make sure that the $table->dropForeign() command was being passed the right relationship name, not necessarily the column name. You do not want to pass the column name, as would be much more intuitive IMHO.
What worked for me was:
$table->dropForeign('local_table_foreign_id_foreign');
$table->column('foreign_id');
So the string I passed to dropForeign() that worked for me was in the format of:
[local table]_[foreign key field]_foreign
If you have access to a tool like Sequel Pro or Navicat, being able to visualize those will be very helpful.
Something that occurred to me was that I didn't know where to put the Schema::table block.
Later I discovered that the key is on the SQL error:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table if exists `lu_benefits_categories`)
So the Schema::table block needs to go in the down() function of the lu_benefits_categories migration and before the Schema::dropIfExists line:
public function down()
{
Schema::table('table', function (Blueprint $table) {
$table->dropForeign('table_category_id_foreign');
$table->dropColumn('category_id');
});
Schema::dropIfExists('lu_benefits_categories');
}
After that, the php artisan migrate:refresh or php artisan migrate:reset will do the trick.
on laravel 8 use dropConstrainedForeignId
(https://github.com/laravel/framework/pull/34806)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddAddressFieldsInEventTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('events', function (Blueprint $table) {
$table->bigInteger('address_id')->nullable();
$table->foreign('address_id')
->references('id')
->on('addresses')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('events', function (Blueprint $table) {
$table->dropConstrainedForeignId('address_id');
$table->dropColumn('address_id');
});
}
}
You can disable relation id first
Schema::disableForeignKeyConstraints();
In Laravel > 8.x you can use the following code to add/remove foreign keys. You will need to drop the foreign key before dropping the column, as otherwise, the foreign key could cause the error your encountering.
Add foreign key
$table->foreignId('pick_id')->constrained('picks')->cascadeOnUpdate()
->cascadeOnDelete();
Drop foreign key
$table->dropConstrainedForeignId('pick_id');