Unable To create new table in database - laravel-5

I created two tables in database. But now I get error to create another one. Third table can not add in the database. But migrate successfully.
I tried all those thinks which are mentioned below:
php artisan make:migration create_book_table
php artisan migrate
after that I get an error:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `Admins` add unique `admins_emai
l_unique`(`email`))
Then I tried:
php artisan migrate:refresh
but it's still not fixed.
Here is the migration file
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBookTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('phone-number');
$table->string('checkIn');
$table->string('checkOut');
$table->string('Room');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('books');
}
}

Put this line of code in your AppServiceProvider.php file in the boot function:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}

Related

Can't rollback with --step a single column

So I just added a colum via php artisan migrate to an orders table.
This colum was added via php artisan make:migration add_balance_to_orders_table --table=orders command.
If I php artisan migrate:rollback --step=1 I get this:
Method Illuminate\Database\Schema\Blueprint::dropColum does not exist.
Bad Method Call: Did you mean Illuminate\Database\Schema\Blueprint::dropColumn() ?
when dropColum is present.
This is how it looks:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddBalanceToOrdersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('orders', function (Blueprint $table) {
$table->float('balance');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('orders', function (Blueprint $table) {
$table->dropColum('balance');
});
}
What's wrong here?
like it says in the error
Method Illuminate\Database\Schema\Blueprint::dropColum does not exist.
Bad Method Call: Did you mean
Illuminate\Database\Schema\Blueprint::dropColumn() ?
you gave the dropColum change it to dropColumn like below
public function down()
{
Schema::table('orders', function (Blueprint $table) {
$table->dropColumn('balance');
});
}
and then run the
php artisan migrate:rollback --step=1
Hope it works for you

Dropping foreign keys in Laravel migration

I have a problem with dropping some foreign keys from my Laravel application. The problem is when I am trying to rollback the migration:
php artisan migrate:rollback
I don't know why I have errors in the console:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'role_user_user_id_foreign'; check that column/key exists (SQL: alter table role_user drop foreign key role_user_user_id_foreign)
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'role_user_user_id_foreign'; check that column/key exists
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'role_user_user_id_foreign'; check that column/key exists
Below I'm showing my migration class:
class UpdateRoleUserTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
schema::table('role_user',function(Blueprint $table){
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('role_id')->references('id')->on('roles');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('role_user', function (Blueprint $table) {
$table->dropForeign('role_user_user_id_foreign');
$table->dropForeign('role_user_role_id_foreign');
});
}
}
My table in the database has been created by migration class:
class CreateRoleUserTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('role_user');
}
}
In all of the >4.0 versions of Laravel, it allows placing column names into an array, which it will then resolve on its own. I tried to find accompanying docs, but they seem to have left it out.
In your update migration, try this:
Schema::table('role_user', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['role_id']);
});
I just had this issue and the problem was due to the fact that $table->dropForeign([column_name]); drops the index and not the column itself. The solution is in the down function drop the index in one block and then drop the actual column in a separate block. They have to be dropped in separate blocks because of something to do with not being able to drop the key in the same connection as where the column is being dropped.
So the down function should look like this:
public function down() {
// drop the keys
Schema::table('role_user', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['role_id']);
});
// drop the actual columns
Schema::table('role_user', function (Blueprint $table) {
$table->dropColumn('user_id');
$table->dropColumn('role_id');
});
}
now you can run php artisan migrate to run the up function and php artisan migrate:rollback to run the down command and the error no longer shows.
I have modified your code below.
Add the onDelete() and onUpdate() to your code.
public function up()
{
Schema::table('role_user',function(Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE')->onUpdate('CASCADE');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('CASCADE')->onUpdate('CASCADE');
});
}
public function down() {
Schema::table('role_user', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['role_id']);
});
}
In Laravel 9.x, you do not need to specify a separate deletion of foreign keys.
<?php
use App\Models\Question;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up(): void
{
Schema::create('answers', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Question::class, 'question_id');
$table->text('correctly');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down(): void
{
Schema::dropIfExists('answers');
}
};

Failing to create new migration

I want to create a new table, users_profile
here is the migration code
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersProfileTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users_profile', function (Blueprint $table) {
$table->increments('id');
$table->string('city');
$table->string('country');
$table->integer('users_id')->unsigned();
$table->foreign('users_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users_profile');
}
}
facing an error
base table or view already exists: 1050 Table "users" already exists
here is the migration code of users
<?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('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Although i am creating a new migration, a new table only i am declaring a primary key of users table as foreign key in users_profiles but facing the above error
help please!
Just a word of advice, Use Laravel Nomenclature to avoid these kind of issues. Always define your table names as plural snakecase and use singular camelcase model to access them.
In your case, change your table name to: user_profiles and use UserProfile model to access it automatically.
Ofocurse you need to change your class name in your migration accordingly: CreateUserProfilesTable
I believe it should resolve your issue.
I believe you are using : php artisan migrate and as users table already exists , running migration for users table again gives that error.
Use: composer dump-autoload and then php artisan migrate:refresh
to rollback all of your migrations and reinstall migrations.
Simple go to your database and drop Schema table ("users") and also remove entry in Schema table ("migrations") and run your migrations again...your problem is solve....

Change tables structures with migration without losing data?

I have read several threads about this but still not exactly my problem.
So in the users table I have a column called role, which is enum type and has two available values: 1 and 2. I set 2 as the default one. Now I want to change it to 1, for example. I created a new migration, ran php artisan migrate and encounter this error:
[Illuminate\DatabaseQueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'role' (SQL: alter table `u
sers` add `role` enum('1', '2') not null default '1')
[PDOException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'role'
Here is code in my CreateUsersTable migration file:
$table->enum('role', ['1', '2'])->default('2');
And I did the same in the new UpdateUsersTable migration file:
$table->enum('role', ['1', '2'])->default('1');
And by the way I can not use php artisan migrate:refresh because it will delete all my data. Where am I doing wrong?
$table->enum('role', ['1', '2'])->default('1')->change();
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Migrations\Migration;
class UpdateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
DB::statement('ALTER TABLE `users` MODIFY `role` DEFAULT 1;');
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
DB::statement('ALTER TABLE `users` MODIFY `role` DEFAULT 2;');
}
}
Adapted from this answer to another question
In order to do this you have to add ->nullable() or ->default() to every field you add to the migration file.
create a new migration file
php artisan make:migration add_role_to_users_table --table=users
open the created migration file (database\migrations\2021_12_01_050851-add_role_tables.php) and add below code.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddRoleToUsers extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->enum('role', ['1', '2'])->default('1')->comment('1 - admin, 2 - normal'); //added
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('role'); //added
});
}
}
?>
Now migration refresh
php artisan migrate:refresh --path=/database/migrations/2021_12_01_050851-add_role_tables.php

Laravel migrations: dropping a specific table

Is there any way/laravel-command to drop a specific table from the production server?
Set up a migration.
Run this command to set up a migration:
php artisan make:migration drop_my_table
Then you can structure your migration like this:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class DropMyTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
// drop the table
Schema::dropIfExists('my_table');
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
// create the table
Schema::create('my_table', function (Blueprint $table) {
$table->increments('id');
// .. other columns
$table->timestamps();
});
}
}
You can of course just drop and not check for existence:
Schema::drop('my_table');
Read further in the docs here:
https://laravel.com/docs/5.2/migrations#writing-migrations
You may also have to consider dropping any existing foreign keys/indexes, for example if you wanted to drop a primary key:
public function up()
{
Schema::table('my_table', function ($table) {
$table->dropPrimary('my_table_id_primary');
});
Schema::dropIfExists('my_table');
}
More in the docs in dropping indexes etc here:
https://laravel.com/docs/5.2/migrations#dropping-indexes

Resources