I been suffering a problem when I run php artisan migrate command - laravel

When I run php artisan migrate command then I see this error. I have an idea for this error is that I deleted the product migration from the migrations table. What can I do to migrate the product table?
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'products' already exists (SQL: create table products(idbigint unsigned not null auto_increm ent primary key, category_id int not null, subcategory_id int not null, product_name ...) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
at F:\xampp\htdocs\suzayetstore\vendor\laravel\framework\src\Illuminate\Datahase\Connection.php:742
F:\xampp\htdocs\suzayetstore\vendor\laravel\framework\src\Illuminate\Database\Connection.php:527 PDOException::("SQLSTATE[42501]: Base table or view already exists: 1050 Table 'products' already exists")

This issue happens because you already have a table in your database and you are trying to migrate the same table without droping(deleting) it. You have 2 options which I have explained below,
If you already dont have any datas in your database tables. Then you can use,
php artisan migrate:fresh
So that all the tables will be deleted and recreated again.
If you need already have datas in any of your table in your database. And you need the datas. You just need to delete the table from database. For that use the query below,
drop table tablename;
And then, run the below command,
php artisan migrate

Related

Illuminate\Database\QueryException SQLSTATE[42S02]: Base table or view not found: 1146 Table

I am facing this error on my website while viewing the product page.
its a e-commerce website
Illuminate\Database\QueryException SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘quietqre_QuietQrewHerbal.product_queries’ doesn’t exist (SQL: select count(*) as aggregate from `product_queries` where `product_id` = 15 and `customer_id` != 9)
Table product_queries in your database quietqre_QuietQrewHerbal is not exist. you need migrate database.
If it's new, use php artisan migrate
and if it's old migration and deleted by mistake or etc... find related migration row in migrations table (showed in image) and delete just that row, and use php artisan migrate

When I try to Migrating my Laravel database I get the error "alter table `users` add unique `users_email_unique`(`email`)"

I have one issue on my Laravel database migration when I try to migrate my database table using this cmd command: PHP artisan migrate then below error is displayed.
alter table `users` add unique `users_email_unique`(`email`)
Do I have to change anything in my user's table file or is there any other setting?
Credit: Lumen 5.6 Migrate Error Specified key was too long max key length is 767 bytes
use Illuminate\Support\Facades\Schema; //AppServiceProvider.php
public function boot(){
Schema::defaultStringLength(191);
}

Artisan skipping migrations

So, I've just pulled down a project in Laravel 5 from Github that I've done no work on before, but I need to set up to add a feature or three.
Problem is, I don't have any of the database tables for this project. That should be easy enough to fix, as the migrations are all there in the proper folder.
I run php artisan migrate and it tells me that there's a problem with one of the migrations trying to update a table that doesn't exist.
The problem is, that isn't the first migration by date. And an earlier migration should create the table it's saying isn't there yet.
Basically, it's something like this:
Migration 1
Migration 2
Migration 3
Migration 4
Migration 5 <--- here's where the error is occurring
Migration 6
etc....
It doesn't appear to be running migrations before the migration that throws an error at all, and it's not writing anything to the migrations table in that schema.
The error(s) I'm getting is:
[Illuminate\Database\QueryException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'table1' doesn't exist (SQL: select * from `table1` where `code` = DEFAULT_ADDRESS limit 1)
and
[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'table1' doesn't exist
Needless to say, I'm a bit confused as to why it would skip migrations, even ones that don't concern that table.
You should run:
php artisan migrate:status
It should show you migrations in the order they should be run. Make sure the order is valid. If it's not probably something was messed up it you might need to alter files to have valid order of your migrations.

PHP Artisan migrate fails

This problem has been solved. Check my post below
I have just installed a fresh Laravel 5.4 project.
As i'm following Jeffrey Way's Laravel from scratch tutorial i get the following errors when i want to migrate my database:
[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 pr
imary key, `name` varchar(255) not null, `email` varchar(255) not null, `passwo
rd` varchar(255) not null, `remember_token` varchar(100) null, `created_at` tim
estamp null, `updated_at` timestamp null) default character set utf8mb4 collate
utf8mb4_unicode_ci)
-
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already
exists
Like i said, it's a fresh installation and i don't know how to fix this. Does anyone of you know?
Thanks in advance!
#ian,
I followed the instructions you gave me but now i get this error:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too l
ong; max key length is 767 bytes (SQL: alter table `users` add unique `users_em
ail_unique`(`email`))
-
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too l
ong; max key length is 767 bytes
How to solve this issue:
This is how i did this, thanks to Ian:
delete database and create a new one
Update env file with new database credentials
Go to App->Providers->AppServiceProvider.php
Add Schema::defaultStringLength(191); to the boot function
make sure to add use Illuminate\Support\Facades\Schema; on the top of the file.
Migrate the database and the problem is solved!
The second error you come across regarding key lengths is a new change in Laravel 5.4. When using the utf8mb4 encoding, varchar fields can only be a max of 194.
You will need to head into your migrations, alter the size of each occurrence of string() and add a length. Optionally you can change your database encoding back to UTF8, however utf8mb4 allows the use of storing emojis.
You can refer to https://laravel-news.com/laravel-5-4-key-too-long-error for more information
When doing php artisan migrate the database can't have the same table name like in migration (except if you are adding just another field in that table)
You must delete all of your tables (drop) and then run php artisan migrate again.

How to migrate schema in Laravel without loss of existing data?

I have table named "user_table" has certain columns and table has filled with data. I want to add some more columns. So how can I migrate that new schema in Laravel without loss of existing data. Even I can not do rollback because when I will do then all stored data will be deleted. So, can somebody please help me to solve this issue ?
You can create new table columns in migrations without loss of data. If you alter a column or drop a column, then of course you will manipulate existing data. As long as you are only adding new columns, then you can run
php artisan make:migration alter_my_table_add_columns
and update your existing schema. Then you can safely run
php artisan migrate
In your new migration you would then add:
Schema::table('my_table', function ($table) {
$table->string('my_new_column');
});
See: https://laravel.com/docs/master/migrations#creating-columns

Resources