I got the package laravel-tags from my favourite package provider spatie into the project to test it. After the first tests I wanted to empty the tables with php artisan migrate:refresh --seed
to run further tests. But I get the exception:
SQLSTATE[HY000]: General error: 1 table "tags" already exists (SQL: create table "tags" ("id" integer not null primary key autoincrement, "name" text not null, "slug" text not null, "type" varchar, "order_column" integer, "created_at" datetime, "updated_at" datetime))
A look into the migration file showed me that there is no down() function of the migration of Spatie. So the data remains persistent and I can’t start a rollback or reset. I don’t like this at all. I added a down function munell, but even there I get the above exception. Hence my two questions:
- Is there a reason not to deliver a down() function?
- What do I have to do to empty the new table?
Thanks in advance, Max
2
Answers
To your second question you can use
php artisan db:wipe
. That will drop all tables successfully.To your first question:
It is not standard to omit the down function in the migration. But why Spatie did it in this case I can only speculate. So it is hard to answer but good that you ask the question. Just add the
down()
function manually. After you have deleted the DB once, it should also work with the future rollbacks and resets.I think it’s common practice to only use
migrate:fresh
which doesn’t run rollbacks and just drops all tables.If you’re doing this any
down()
functions go unused, untested and are easily neglected.Testing by default performs a
migrate:fresh
so you have extra work to do if you want to run tests and rely on rollbacks.Effectively Laravel encourages regular nuking of the database – this is simplicity is helpful in some use cases.
If nuking the database works for you – run
migrate:fresh
If you need to preserve some tables – provide your own
down()
method and see Laravel exclude table from dropping