简体   繁体   中英

How can I do a migration in laravel 5.5?

I've created an Auth project with laravel 5.5 and created new migration and when I migrate I receive this error msg:

In Connection.php line 647:

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,
      `username` 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` timestamp null,
      `role` int not null
      ) default character set utf8mb4 collate utf8mb4_unicode_ci
)

In Connection.php line 449:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists

i try php artisan migrate --force and php artisan migrate:rollback

and try to drop all tabels and migrate it again and still ahve this error

It sounds like you're using a database that already has a users table, but no migration table. Therefore, when you run the migration, it's attempting to create the users table again.

There are two things you can try:

1) Try setting up a new (empty) database in MySQL and update the settings in your .env file to point to this new DB. Run php artisan migrate and see if your migrations are correctly applied.

2) Using your existing database, delete / drop all tables (make a backup of the data if you want to retain it), and then run php artisan migrate and see if that fixes the issue.

to understand the problem and find a solution you need to understand how migration works. migrations work on a creation date basis way. whenever a migration is created the current time stamp is append to the file name. and migration runs according to that time stamp. NOW LARAVEL creates two migrations named 2014_10_12_000000_create_users_table.php and 2014_10_12_100000_create_password_resets_table.php. as the rule of laravel whenever you are going to run migration these two files are going to migrate but users table already exists. delete these to files and hope your problem will be solved. one thing, if you are handling databse in laravel using schema and migrations do not ever delete db manually. it will cause you unnecessary problems. the migration table keeps track of all the migrations you run and deleting db manually will break that track of keeping data in migrations table. so next time you run a schema, laravel would not be able to understand whats going on and will create errors. hope that will help you.

Have you created a new migration for the table 'users'?

Laravel by default when a new project is created will produce a migration for a users table. This will mean if you create a new migration with the 'users' table it will try to create the table twice hence causing the error.

You can either remove the laravel created migration, change the name of your new table or modify the laravel migration rather than creating your own.

after reading error msg in CMD (DOS) and check laravel documentation

error in length i dont know if any one see this error before or not but when i edit length its work

i edit 3 migration as below :-

1 -1- Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('username')->unique(); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); $table->integer('role'); });

and now its

        Schema::create('users', function (Blueprint $table) {
        $table->increments('id')->autoIncrement();
        $table->string('name',200);
        $table->string('username',50)->unique();
        $table->string('email',100)->unique();
        $table->string('password',50);
        $table->string('role',50);
        $table->rememberToken();
        $table->timestamps();

    });

number 2 it was

 Schema::create('password_resets', function (Blueprint $table) { $table->string('email')->index(); $table->string('token'); $table->timestamp('created_at')->nullable(); });

and now its :-

        Schema::create('passwordreset', function (Blueprint $table) {
        $table->string('email',200)->index();
        $table->string('token',200);
        $table->timestamp('created_at')->nullable();
    });

number 3 it was :-

3- Schema::create('tweets', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned()->index(); $table->text('text'); $table->timestamps(); });

now its :-

        Schema::create('tweets', function (Blueprint $table) {
        $table->increments('id')->autoIncrement();
        $table->string('user_id',50)->index();
        $table->string('twetts',255);
        $table->timestamps();
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM