Artisan skipping migrations - laravel

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.

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

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

All artisan commands throw PDOException

I have run into an extremely weird problem. At first I encountered it during migrations, but since then it is happening in all commands. Even composer install and php artisan serve tell me this:
[Illuminate\Database\QueryException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mydb.patent_examination_types' doesn'
t exist (SQL: select * from `patent_examination_types`)
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mydb.patent_examination_types' doesn'
t exist
[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mydb.patent_examination_types' doesn'
t exist
I've tried composer dump-autoload, which says Generating autoload files and then if I say php artisan serve I get the same result.
I'm quite puzzled!
It seems that your have deleted your 'patent_examination_types' table from the database but this table is using in your code.
Check your code for 'patent_examination_types' table, Maybe you have write this table in you some controller or model, Search this table throughout your code and comment it..
Hope it helps.

Database Migration Laravel 5.2 Arrange Table

I have a create_users_table at the top and with foreign key to roles table but the location of roles table is at the bottom of create_users_table. So what happen is the create_users table can't see the roles table because it's not yet created. See attached image:
Do I need to recreate a users table so that it will come up below to roles table?
Do I need to recreate a users table so that it will come up below to
roles table?
You can't add a foreign key to a table that does not yet exist. If you want to manage your database through migrations, you do need to put them in the right order.
However, you can also add it as a separate migration, so that you have
create users table
create roles table
alter users table to add role_id field and foreign key
That way you don't have to rearrange existing migrations if you e.g. need to make changes to the database mid-project.
if its just a beginning of the project and if you are not using database migrations versioning yet you could just rename the datetime timestamp at migrations filename to rearrange the migrations. something like this would rearrange migrations to the required order. see i changed the year of 2016_08_13_001252_create_roles_table.php to 2014_08_13_001252_create_roles_table.php.
2014_08_13_001252_create_roles_table.php
2014_10_12_000000_create_users_table.php
2014_10_12_100000_create_password_resets_table.php
2016_08_08_005720_create_messages_table.php
migrations run in the datetime order of the filenames. Only the new migrations are run at each run. so, if you bring such changes to the files like renaming the file you need to drop database and create again, then run the migrations.
sometime after doing this you might need to run following commands in the project
composer dump-autoload and/or php artisan clear-compiled.
if you are running the migrations verisoning?
migrations are something related to databases and its not meant for dropping all the tables and recreating each and every time when you need to bring a change. If the database is live or if you have completed major database design you could use versioning. one of the purpose of migrations made in that way (datetime order and only new ones are migrated each time) is that only changes are brought to the old migrations created.

In weird state with foreign key after migrations bug

I'm working on my first project using laravel 5. All my migration scripts create tables except for one that adds a foreign key constraint afterward (so both tables are in place before it runs). I found a bug in one of my migration scripts (missing parens made field non-nullable instead of nullable), so after fixing that, I tried doing php artisan migrate:refresh
I hit several errors, so I (mistakenly) decided to get to a fresh state by dropping the tables (using PostgreSQL Studio, via Heroku), but now I'm stuck in a weird state:
When I try php artisan migrate or php artisan migrate --force, it says "Nothing to migrate".
but when I try to rollback or reset or refresh, I get an error:
Undefined table: relation 'users' does not exist (SQL:alter table "users" drop constraint person_id)
That error is from that one migration script that adds the foreign key- I got the overall syntax from here- (http://laravel.com/docs/5.0/schema#foreign-keys)- scream out if wrong:
class AddConstraintToUsers extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->foreign('person_id')
->references('id')
->on('people');
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropForeign('person_id');
});
}
}
More info: based on comment below I deleted the rows from the migrations table so they won't be listed as having already run (in hopes of helping the "Nothing to migrate"). After doing that and trying php artisan migrate I get the same 'relation "users" does not exist" error mentioned above, and now the migrations table shows a 'batch' value of 1 for create_users_table, and 2 for all the others.
Even crazier: since artisan runs migrations in order and thought it had nothing to run (but had been blocked from creating the users table), I created a new migration script to execute last, where the contents were creating that users table. At least it stopped telling me 'Nothing to migrate', BUT it gives a 'duplicate table' error for the OTHER table (called 'updates'), not the one I've actually tried to create twice :p
How can I get back to a normal place-(where either artisan finds my migration scripts to run fresh, or stops trying to rollback the change for the table that's no longer there)- I promise I've learned my lesson to not drop tables. Unfortunately it looks like this problem is less-google-able than usual (or I'm not using the right terms)- any help is greatly appreciated!!
In the end, the way out was to delete the migrations table too (not sure why deleting all the rows in it hadn't been sufficient, but at least I'm back in business now):
I dropped all the tables in my database, including the migrations table
I recreated the migrations table with artisan like so: php artisan migrate:install
Then migrations ran successfully, no problem: php artisan:migrate
Everything working again- hooray!

Resources