Estoy tratando de usar Laravel Migration para crear tablas SQL pero no me deja.
Aquí está el error:
SQLSTATE[42S01]: La tabla o vista base ya existe: 1050 La tabla 'mytable' ya existe
Aquí está mi código:
Schema::create('mytable', function (Blueprint $table) { $table->increments('id'); $table->foreign('othertable_id') ->references('id')->on('othertable') ->onDelete('cascade'); $table->string('variable'); $table->timestamps(); }); } { Schema::drop('mytable'); }También verifiqué si otras migraciones se llaman "mytable", pero no lo son, por lo que no estoy seguro de dónde proviene esto.
Intenté lo siguiente:
Primero
php artisan migrate:refreshque me dio un error
Y luego eliminé toda la base de datos por completo y usé:
php artisan migrateY todavía tengo el error
En laravel 5.5 y > puedes usar:
$ php artisan migrate:fresh // ( not migrate:refresh wich is a different command)Este comando primero elimina todas las tablas y luego vuelve a ejecutar todas las migraciones
Si tiene la tabla en la base de datos y no desea migrarla y crearla, puede ignorarla marcando Schema::hasTable antes de crearla.
public function up() { if(Schema::hasTable('products')) return; //add this line to migration file Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); });}
No eliminé la entrada de migración ni eliminé la tabla manualmente de la base de datos. En mi caso, la solución es abrir el tinker del compositor.
$ php artisan tinker >>> Schema::drop('users') >>> Schema::drop('password_resets') >>> Schema::drop('orders') >>> exit php artisan migrateAquí está el resultado de los comandos anteriores ejecutados
nishanth@localhost:~/Desktop/html/hutch$ php artisan migrateEn la línea 647 de Connection.php: SQLSTATE[
name]: la tabla base o la vista ya existe: 1050 La tabla 'usuarios' ya existe (SQL: crearusersde la tabla (id. nulo,passwordvarchar (255) no nulo,remember_tokenvarchar (100) nulo,created_attimestamp null,updated_attimestamp null) conjunto de caracteres predeterminado utf8mb4 cotejar utf8mb4_unicode_ci)En Connection.php línea 449: SQLSTATE[42S01]: La tabla base o la vista ya existe: 1050 La tabla 'usuarios' ya existe
nishanth@localhost:~/Desktop/html/hutch$ php artisan migrate:rollback Nothing to rollback. nishanth@localhost:~/Desktop/html/hutch$ php artisan tinker Psy Shell v0.8.17 (PHP 7.1.20-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman >>> Schema::drop('users') => null >>> Schema::drop('password_resets') => null >>> Schema::drop('orders') => null >>> exit Exit: Goodbye. nishanth@localhost:~/Desktop/html/hutch$ php artisan migrate Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table Migrating: 2018_08_18_071213_create_orders_table Migrated: 2018_08_18_071213_create_orders_table nishanth@localhost:~/Desktop/html/hutch$ También defina el método down() , si no existe.
De lo contrario, se mostrará
SQLSTATE[42S02]: Tabla base o vista no encontrada: 1051 Tabla desconocida 'XYZ.ABC' (SQL: soltar tabla
ABC)
/** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('ABC'); }