Migration files in laravel is used to create the tables in the database, right? But when ever I try to migrate it gives me this error:
C:\xampp\htdocs\app>php artisan migrate
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (
id int unsigned not null auto_increment primary key, name varchar(255) not null, email varchar(255) not null,
password varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at tim
estamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
and I created a new migration file and it's called test. I know users already exist but I want create the new table I created which is called test.
here is the migration file i am going to use to create my table but wont create:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTestsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('tests');
}
}
here is the users migration file that tells me it exist:
<?php
use Illuminate\Support\Facades\Schema;
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->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
here is the password migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePasswordResetsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('password_resets');
}
}
here is the dummies migration file i have that i didn't talk about because i thought it is not the problem:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDummiesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('dummies', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('body');
$table->timestamp('date'); //if you dont put name for the timestamp it will create: create_at and update_at fields.
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('dummies');
}
}
and sorry for keeping you guys waiting it took me long to find the edit button and fix the spacing of my code. It is my first time using stack over flow.
The migrations table in your database is out of sync with the rest of your database. Laravel is trying to re-run a migration to create the users table and failing.
If this is an entirely new project, you could delete all tables from your database and re-run all migrations. Alternatively, fix your migration table so that it accurately reflects the state of your database.
In your laravel/app/providers/AppServiceProvider.php
add the following code inside boot
Schema::defaultStringLength(191);
Then try php artisan migrate:refresh
or best
drop all tables and run php artisan migrate again
Add following code in the start of your up function() of all tables
public function up()
{
// Add this line
Schema::dropIfExists('users');
// from you down method
Schema::create('users', function (Blueprint $table) {
...........
});
}
Try this...
*// AppServiceProvider.php
use Illuminate\Support\Facades\Schema;
function boot()
{
Schema::defaultStringLength(191);
}
*
Related
I am trying to add foreign key constraints to my database tables via Laravel Migrations, but I always get an error like this:
Illuminate\Database\QueryException :
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
(SQL: alter table `tasks` add constraint `tasks_task_list_id_foreign` foreign key (`task_list_id`) references `task_lists` (`id`))
The migration for the tasks table looks like this:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTasksTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->integer('task_list_id')->unsigned()->index();
$table->string('task');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('task_list_id')->references('id')->on('task_lists');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('tasks');
}
}
And that's the task_lists table:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTaskListsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('task_lists', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->string('name');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('task_lists');
}
}
I can't figure out the problem and would highly appreciate any kind of help.
Thank you in advance!
You have to check the order of your migrations. Obviously, you have to run de Users table migration before the Tasks table migration. If the problem persist, try to make something like this inside the up() method and after the (Schemma::create):
Schema::table('task_lists', function($table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
I am very new at Laravel! But I'm trying. Here is were I could use some help;
I have a posts table. I have a user table. But I forgot to add a foreign key in the posts table that links to the user id.
The create users table migration:
use Illuminate\Support\Facades\Schema;
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->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
The create posts table migration:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
I have created the new migration file:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AlterTablePostsAddForeignUser extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
// I HAVE NO IDEA WHAT TO DO NEXT
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
I have no idea how to fill up the "public function up()" method here! If anyone can help! It will be much appreciated!
I tried this and this worked for me.
Schema::table('posts', function (Blueprint $table) {
$table->integer('user_id')->nullable()->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
Here When you adding foreign key, by default it will have a Null value.This 'null referencing' may cause the issue. Using nullable() foreign key will avoid that issue.
See the docs here
You need to install doctraine/dbal before modifying a column, be sure to add the doctrine/dbal dependency to your composer.json file. The Doctrine DBAL library is used to determine the current state of the column and create the SQL queries needed to make the specified adjustments to the column and the you should try what I found on this SO post
Schema::table('posts', function (Blueprint $table) {
$table->integer('user_id')->unsigned()->change();
$table->foreign('user_id')->references('id')->on('users');
});
change() method for change structure of column
after this run the artisan command
php artisan migrate
if this doesn't work for you shoot the comment here! :)
I'm having issues with the migration registering in the migrations table and I'm getting the following error when I run php artisan migrate:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key
constraint (SQL: alter table `surgeon_surgeon_specialty` add
constraint `surgeon_surgeon_specialty_surgeon_id_foreign` foreign key
(`surgeon_id`) references `surgeon` (`id`) on delete cascade)
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
Here's my files currently:
Surgeons Table Migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSurgeonsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('surgeons', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('surgeon_name', 30)->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('surgeons');
}
}
Surgeon Specialties Table Migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSurgeonSpecialtiesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('surgeon_specialties', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('surgeon_specialty_name');
$table->timestamps();
$table->unique(['surgeon_specialty_name','user_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('surgeon_specialties');
}
}
Then I used the Laravel-5-Generators-Extended package to generate the
Surgeon Surgeon Specialty Table Migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSurgeonSurgeonSpecialtyPivotTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('surgeon_surgeon_specialty', function (Blueprint $table) {
$table->integer('surgeon_id')->unsigned()->index();
$table->foreign('surgeon_id')->references('id')->on('surgeon')->onDelete('cascade');
$table->integer('surgeon_specialty_id')->unsigned()->index();
$table->foreign('surgeon_specialty_id')->references('id')->on('surgeon_specialties')->onDelete('cascade');
$table->primary(['surgeon_id', 'surgeon_specialty_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
// Schema::drop('surgeon_specialty_surgeon');
Schema::drop('surgeon_surgeon_specialty');
}
}
But changed it to this per a friends suggestion:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSurgeonSpecialtiesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('surgeon_specialties', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('surgeon_specialty_name');
$table->timestamps();
$table->unique(['surgeon_specialty_name','user_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('surgeon_specialties');
}
}
The table does migrate...
...however, I am still getting the error. Any help would be much appreciated.Thanks!
I had this issue on Laravel 8. I'm not sure if it's relevant to this post or not. This happens when you create the migration for the many before the one part of one-to-many relationship. In your case I guess you created migration for items before users. Simply rename your migration files according to your relationships. Your surgeon migration date should be earlier to be created before surgeon_surgeon_specialty table.
Also I suggest to declare table name of Surgeon model:
class Surgeon extends Model {
public $table = 'surgeons';
}
This is because I see Laravel couldn't determine your table name based on your model class.
I'm new in laravel, anyone know how to see database table which has been created? I've been migrate the database and stuck after create table using schema builder. This is my code
<?php
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($table)
{
$table->increments('id');
$table->string('nim');
$table->string('name');
$table->string('password');
$table->string('level');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
The new table name will be "users" because you have given 'users' as the name in the Schema::create function.
You can view the table from phpmyadmin if you are using the mysql database, which is sat as the default database for laravel.
I have two tables, transactions, and payments, which already exist from past migrations. When I try to make a pivot table between them, I get an error for the foreign key OF THE TRANSACTIONS ONLY. The other foreign key, on the "payments" table, is created just fine. This is absolutely baffling me, and none of the previous discussions I have found on this error have solved the problem.
The Error (for reference, MAccounting is the name of the database):
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'MRAccounting.#sql-
563_2d7' (errno: 150) (SQL: alter table `payment_transaction` add constrain
t payment_transaction_transaction_id_foreign foreign key (`transaction_id`)
references `transactions` (`id`))
*Despite what the error seems to say, the payment_transaction table is created successfully, along with the foreign key for the payments table; the only one that breaks is the foreign key for the transactions.
The Transaction Table:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateTransactionsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('transactions', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->decimal('balance', 7,2);
$table->integer('account_id');
$table->integer('pending_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('transactions');
}
}
The payments table:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreatePaymentsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('payments', function(Blueprint $table)
{
$table->increments('id');
$table->integer('account_to');
$table->integer('account_from');
$table->decimal('amount',7,2);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('payments');
}
}
The Pivot table:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreatePaymentTransactionTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('payment_transaction', function(Blueprint $table)
{
$table->increments('id');
$table->unsignedInteger('payment_id');
$table->unsignedInteger('transaction_id');
$table->timestamps();
// $table->foreign('payment_id')->references('id')->on('payments');
// $table->foreign('transaction_id')->references('id')->on('transactions');
});
Schema::table('payment_transaction', function(Blueprint $table){
$table->foreign('payment_id')->references('id')->on('payments');
$table->foreign('transaction_id')->references('id')->on('transactions');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('payment_transaction');
}
}
******************* GOT IT WORKING, BUT I STILL NEED TO FIGURE OUT HOW THIS HAPPENED ********
Unfortunately, a clean install solved this problem. That's fine and dandy, and I can keep developing now, but doing a clean install like that is not necessarily a convenience I have in a production environment. I need to figure out what caused this/how to recreate it.
you can fix all this mess by making payment_transaction.payment_id of type INT(10) unsigned. in this case payments.id INT(10) AUTO_INCREAMENT will be equal and your references will work.
Use the flag unsigned() on your primary and foreign keys.
For your pivot table use this migration:
public function up() {
Schema::create('payment_transaction', function(Blueprint $table) {
$table->increments('id');
$table->unsignedBigInteger('payment_id');
$table->foreign('payment_id')->references('id')->on('payments');
$table->unsignedBigInteger('transaction_id');
$table->foreign('transaction_id')->references('id')->on('transactions');
});
}
Why don't you try this:
$table->integer('payment_id')->unsigned();
$table->integer('transaction_id')->unsigned();