PHP Artisan migrate fails - laravel

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.

Related

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

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

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);
}

SQLSTATE[23505]: Unique violation In-spite of ID set to auto_increment

I have created migration from the PHP artisan command and it had created a table in my Postgres database, with id set to auto_increment.
I have made some seeder in laravel and three rows of data are fed to the previously created table through php artisan db:seed command.
When I am inserting data through some form in the same table, it is giving me an error.
error:SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "roles_pkey" DETAIL: Key (id)=(1) already exists. (SQL: insert into "roles" ("name", "guard_name", "updated_at", "created_at") values (staff, web, 2019-07-03 07:38:37, 2019-07-03 07:38:37) returning "id")
Sequences are objects that return a value that is one greater on each request, regardless of which transaction it was called on. A sequence by default starts at 1 and can be applied to a table, or many tables, so it cannot know how many values there are in your table already. If you want your insert to work you will need to manually set it.
SELECT setval('roles_id_seq', (SELECT coalesce((SELECT max(id) from roles),1)))
This query is assuming that the sequence used was created on the column "id" on the "roles" table, if not the sequence name can be found by checking that columns DDL e.g. NOT NULL DEFAULT setval('the_sequence_name') and use that to set the value.

How to add unique constraint to column with LongText datatype in laravel 5.7

Can i add unique constraint to LongText column in laravel.
I mean, for example i have table having body column with LongText datatype, then how i can make it unique.
$table->LongText('body')->unique();
When i do this it will generate the following SQL error.
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1170 BLOB/TEXT column 'long_questi
on' used in key specification without a key length (SQL: alter table `long_questions` add unique `long_questions_long_question
_unique`(`long_question`))
I need need a solution for this to make a LongText column unique...
please give me any suggestion.
According to me. You can't make text to unique.
string can be .
$table->string('sensitive_string', 255)->unique();

H2 Database - Unknown data type of MySQL script

I'm new in using in-memmory database.
I tried to use H2 database for developing project with spring boot, spring data JPA, but i got a problem when initialize application.
Caused by: org.h2.jdbc.JdbcSQLException: Unknown data type: "FK_PERSON__PERSONTYPE_IDX"; SQL statement:
Because this script was exported from MySQL. So i thinked there are some wrong syntax which H2 does not understand
For example, this is a part of script:
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(255) NOT NULL,
`lastname` varchar(255) DEFAULT NULL,
`type` int(11) NOT NULL,
`address` text,
PRIMARY KEY (`id`),
KEY `fk_person__persontype_idx` (`type`),
CONSTRAINT `fk_person__persontype` FOREIGN KEY (`type`) REFERENCES `persontype` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' ';
And i tried some solutions from these:
Convert MySQL script to H2
http://matthewcasperson.blogspot.de/2013/07/exporting-from-mysql-to-h2.html
Replace symbols with double quotes, single quotes,... even not use quote at all but not working.
Please show me why?
Thank you.
I had a similar issue.
Removing the "KEY" line, which in your example corrosponds to:
KEY `fk_person__persontype_idx` (`type`),
worked for me.

Resources