I have 2 tables, badges and counselors. All of my tests were green. I added a third table, a pivot table, named badge_counselor. Here is the migration:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBadgeCounselorTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('badge_counselor', function (Blueprint $table) {
$table->increments('id');
$table->integer('badge_id')->unsigned();
$table->integer('counselor_id')->unsigned();
$table->foreign('badge_id')->references('id')->on('badges')->onDelete('cascade');
$table->foreign('counselor_id')->references('id')->on('counselors')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('badge_counselor');
}
}
When i run php artisan migrate / php artisan migrate:refresh / php artisan migrate:rollback, everything works fine. Howevever, when i run my unit tests, ALL of them fail. And each one returns the error message:
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 table "badge_counselor" already exists (SQL: create table "badge_counselor" ("id" integer not null primary key autoincrement, "badge_id" integer not null, "counselor_id" integer not null, foreign key("badge_id") references "badges"("id") on delete cascade, foreign key("counselor_id") references "counselors"("id") on delete cascade))
or simply:
PDOException: SQLSTATE[HY000]: General error: 1 table "badge_counselor" already exists
Judging by the error message a assume that the table is not being dropped correctly, but when i run the migrate commands from the terminal they are perfect. I have tried dropping the migrations table, all the tables, and even the entire database and creating it again and nothing seems to work.
Thanks.
Are you using MySQL? If so, probably try deleting the entire database and create it again but make sure that your database is in InnoDB and not MyISAM. For this, change the default_storage_engine to InnoDB in the Variables section in phpMyAdmin.
[Solution] Not exactly sure what was going on but i deleted the sqlite file that i was using for phpunit, then ran them again.
Related
I am running exiting laravel project in homestead. When run php artisan migrate get the error.
Here is full error.
In Connection.php line 664:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forum.chanel' doesn't exist (SQL: select * from `chanel`)
In Connection.php line 326:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forum.chanel' doesn't exist
This is my chanel table
public function up()
{
Schema::create('chanels', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('chanels');
}
Why i getting the error and how can i solve this error?
Get the error when run composer update
You need to check your migration table to see if this migration was already run in the past, it is commong that developers change the code after they run the migration.
Or, you might have some even / interception that is running the query select * from chanel just before the migration running, and it making the migration to fail.
It looks like you're model name and table name are not in sync,
Before this remove all tables from DB it could be problem or migration arrangement in migrations table run composer dumpa then
Try updating your model by specifying $table name,
class Chanel extends Model{
public $table = "chanels";
First need to fresh migration type below command
php artisan migrate:fresh
then run new model with migration file with below command
php artisan make:model chanel -m
with this command auto create a migration file and model file
edit migration file location in {your app}\database\migrations
$table->string('title');
$table->string('slug');
added above line into public function up(){ // code }
now run migration type below code
php artisan migrate
I this this will help you
I'm trying to create foreign keys in Laravel however when I migrate my table using artisan i am thrown the following error:
λ php artisan migrate
Migration table created successfully.
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `fees` add constraint `fee
s_fee_type_id_foreign` foreign key (`fee_type_id`) references `feetypes` (`fee_type_id`))
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
My migration code is as so:
fees
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFeesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('fees', function (Blueprint $table) {
$table->increments('fee_id');
$table->integer('academic_id')->unsigned();
$table->integer('level_id')->unsigned();
$table->integer('fee_type_id');
$table->string('fee_heading', 100)->nullable();
$table->float('amount', 8, 2);
$table->foreign('academic_id')->references('academic_id')->on('academics');
$table->foreign('level_id')->references('level_id')->on('levels');
$table->foreign('fee_type_id')->references('fee_type_id')->on('feetypes');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('fees');
}
}
fesstypes
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFeetypesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('feetypes', function (Blueprint $table) {
$table->unsignedinteger('fee_type_id');
$table->string('fee_type', 100);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('feetypes');
}
}
Any ideas as to what I've done wrong, I want to get this right now, as I've got a lot of tables I need to create e.g. Users, Students, Leves, etc. Ideally I want to create tables which hold this data with the foreign keys, i..e fees and feetypes.
Hope someone can help me to get started.
Replace this line:
$table->integer('fee_type_id');
With this:
$table->unsignedInteger('fee_type_id');
In CreateFeesTable Migration.
Check all data types matches for the referenced data types.
public function up()
{
Schema::create('fees', function (Blueprint $table) {
$table->increments('fee_id');
$table->unsignedInteger('academic_id');
$table->unsignedInteger('level_id');
$table->unsignedInteger('fee_type_id');
$table->string('fee_heading', 100)->nullable();
$table->float('amount', 8, 2);
$table->foreign('academic_id')->references('academic_id')->on('academics');
$table->foreign('level_id')->references('level_id')->on('levels');
$table->foreign('fee_type_id')->references('fee_type_id')->on('feetypes');
});
}
check level_id in levels , academic_id in academics ,fee_type_id in feetypes are also unsignedInteger or autoincrement and change your table creating script to above one.
CreateFeesTable.php
change
$table->integer('fee_type_id');
to
$table->unsignedInteger('fee_type_id');
CreateFeetypesTable
change
$table->unsignedinteger('fee_type_id');
to
$table->increments('fee_type_id'); or
$table->integer('fee_type_id');
You must first create an index on the referenced column, i.e. fee_type_id in feetypes table.
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.
From MySQL Reference Manual
This is my migration file
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSessionsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->unique();
$table->text('payload');
$table->integer('last_activity');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('sessions');
}
}
It was created when i ran
php artisan session:table
The database connection is correctly configured since the project is writing and reading from it without problem.
From the root directory of my project i run
php artisan migrate
And i get the following error messages
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'sessions' already exists (SQL: create table `sessions` (`id` varchar(255) not null, `payload` text not null, `last_activity` int not null) default character set utf8 collate utf8_unicode_ci)
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'sessions' already exists
I know i should run
composer dump-autoload
But i've tried with and without it and i got the same results.
I've also tried the Blueprint type before the $table argument.
It bahaves as if it was accessing some different database than the one it uses when i ran the project
Delete the migration if you haven't added data in it. Or do php artisan migrate:rollback.Then php artisan config:clear and then restart your server. Then rerun the migration
I was able to solve this issue with:
php artisan migrate:fresh
Im trying to create a foreign keys using artisan, but this error show up.
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `comments` add constraint `comments_comment_lot_id_foreign` foreign key (`comment_lot_id`) references `lots` (`lot_id`
) on delete cascade)
This is my migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->text('comment');
$table->integer('comment_lot_id')->unsigned();
$table->timestamps();
});
Schema::table('comments', function ($table) {
$table->foreign('comment_lot_id')->references('lot_id')->on('lots')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropForeign(['comment_lot_id']);
Schema::dropIfExists('comments');
}
}
in the lots table i use lot_id as id it model Lot.php i add:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Lot extends Model {
protected $primaryKey = 'lot_id';
}
Any idea how can i resolve this error?
Apply these rules below to your migration files:
[1]
The parent, pivot table(s) must be based on engines that supports
foreign key referencing (e.g InnoDB for mysql).
Do $table->engine = “InnoDB”;
in your migration file, right before other column definitions.
I observe laravel always default to MyISAM hence this line is a must.
[2]
The referenced columns in the parent must be a primary or unique
key(s).
These declarations in the parent table are fine:
$table->increments(“id”); means column “id” is referencable
$table->column_type(“column_name”)->unique(); means column “column_name” is referencable
[3]
The pivot table column must be of the same type as that of its
referenced parent table column.
So for example, the pivot table column that should reference increments(“id”) must of type of unsignedInteger.
If parent table is type char(20), then pivot table column used to reference it must be type char(20) as well.
Having done all three above, define your foreign key relationship as appropriate.
Looks like this was not the problem for you, but I arrived at this same error in Laravel 5.8 and found an interesting issue: Laravel now defaults the 'id' column to 'bigIncrements' instead of just 'increments'. So instead of referencing it with 'integer' like before, you have to reference it with 'bigInteger'.
If your parent table looks like this:
$table->bigIncrements('id');
Then the child migration needs to look like this:
$table->bigInteger('parent_id')->unsigned()->index();
$table->foreign('parent_id')->references('id')->on('parent');
Hopefully this helps anyone else encountering this problem in 5.8 and beyond.
Quoting this answer:
To find the specific error run this:
SHOW ENGINE INNODB STATUS;
And look in the LATEST FOREIGN KEY ERROR section.
It may be a problem of type. comment_lot_id must be the exact same type as lot_id. Maybe one is signed and the other unsigned.
I am using Laravel 5.2. I did create migration file with php artisan make:migration create_users_table command. File created successfully and I did update file with this code:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('partner_id');
$table->string('social_id');
$table->integer('device_id');
$table->integer('gcm_id');
$table->integer('user_role');
$table->string('social_media');
$table->string('display_name');
$table->string('first_name');
$table->string('last_name');
$table->integer('status');
$table->string('email')->unique();
$table->string('password');
$table->string('profile_pic');
$table->string('gender');
$table->float('height');
$table->float('weight');
$table->string('dob');
$table->bigInteger('mobile');
$table->string('city');
$table->string('state');
$table->integer('zip');
$table->string('country');
$table->float('latitude');
$table->float('longitude');
$table->float('gps_status');
$table->string('favorite_movie');
$table->string('favorite_food');
$table->string('favorite_weather');
$table->integer('login_status');
$table->string('active');
$table->string('token');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}`
for running migration i use this 'php artisan migrate' command. and error occurs
'Migration table created successfully.
[Illuminate\Database\QueryException] SQLSTATE[HY000]: General
error: 1025 Error on rename of '.\laravel\users' to
'.\laravel#sql2-1d88-70' (errno: 190 - Operation not allowed when
innodb_forced_recov ery > 0) (SQL: alter table users add unique
users_email_unique(email))
[PDOException] SQLSTATE[HY000]: General error: 1025 Error on
rename of '.\laravel\users' to '.\laravel#sql2-1d88-70' (errno: 190 -
Operation not allowed when innodb_forced_recov ery > 0)'
After that I used rollback command php artisan migrate:rollback but message shows nothing to rollback and when I want to create second table the message shows the user table already exist.
I believe that the problem with the rollback is that since the migration hasn't finished correctly, the process to add the migration in the db table (as a final step) has not been triggered and therefore the artisan command cannot revert it. To solve this I would suggest you to manually delete the table from the DB.
The problem thought is on the migration itself, on top of my head I can't remember if a fresh installation of Laravel already comes with a users table, check that first of all. If it does then u need to alter the table instead of creating it. If not, I can;t see any obvious reason for the migration to fail.