BigSpender's avatar

Base Table not found during composer install

Hi all!

During 'composer install' I getting this issue. I researched a lot of topics, didn't find relation database in AppServiceProvider. Adjusted .env to correct database connection settings (but earlier this command processed without this).

But I cannot understand, why it is happening during composer install...

I am use Laravel 8.x.

Thanks for any help and advise.

Illuminate\Database\QueryException 

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'project.categories' doesn't exist (SQL: select `id`, `template` from `categories`)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667▕         // If an exception occurs when attempting to run a query, we'll format the error
    668▕         // message to include the bindings with SQL, which will make this exception a
    669▕         // lot more helpful to the developer instead of just the database's errors.
    670▕         catch (Exception $e) {
  ➜ 671▕             throw new QueryException(
    672▕                 $query, $this->prepareBindings($bindings), $e
    673▕             );
    674▕         }
    675▕ 

  • A table was not found: You might have forgotten to run your migrations. You can run your migrations using `php artisan migrate`. 
    https://laravel.com/docs/master/migrations#running-migrations

  1   [internal]:0
      App\Console\Commands\Parser::__construct(Object(App\Services\Image), Object(App\Services\Category), Object(App\Services\Brand))

      +12 vendor frames 
  14  app/Services/Category.php:46
      Illuminate\Database\Eloquent\Model::all()
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
0 likes
26 replies
Sinnbeck's avatar

Can you please post your composer.json file contents?

MichalOravec's avatar

Somewhere in your app you have something like this

 DB::table('categories')->select('id', 'template')->get();

// or

Category::select('id', 'template')->get();

To avoid this kind of problems you can use

use Illuminate\Support\Facades\App;

if (! App::runningInConsole()) {
    // your code
}

And the problem is that the database table categories doesn't exist.

6 likes
BigSpender's avatar

composer.json

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.3",
        "alex.oleshkevich/fast-xml-parser": "^1.0",
        "fideloper/proxy": "^4.2",
        "fruitcake/laravel-cors": "^1.0",
        "guzzlehttp/guzzle": "^7.0.1",
        "intervention/image": "^2.5",
        "laravel/framework": "^8.0",
        "laravel/tinker": "^2.0",
        "laravel/ui": "^3.0",
        "laravelcollective/html": "^6.1",
        "mbezhanov/faker-provider-collection": "^1.2",
        "predis/predis": "^1.1",
        "spatie/url": "^1.3"
    },
    "require-dev": {
        "barryvdh/laravel-debugbar": "^3.5",
        "facade/ignition": "^2.3.6",
        "fzaninotto/faker": "^1.9.1",
        "mockery/mockery": "^1.3.1",
        "nunomaduro/collision": "^5.0",
        "phpunit/phpunit": "^9.0"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories",
            "app/Services"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\Foundation\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}
BigSpender's avatar

you are right. But this service uses on of my php artisan commands at least, not only composer install. I need to find out more delicious way.

Sinnbeck's avatar

Can you show the code that is getting run?

BigSpender's avatar
$categories = \App\Models\Category::all(['id', 'template'])->toArray();

I figured out it is issue of that. Why?

BigSpender's avatar

Anyway, when I replace this to empty array, it works.

BigSpender's avatar

I just created class like app/Services/Category.php . Why it going to this class when I run composer?

MichalOravec's avatar

If you need only those 2 columns you can use

$categories = \App\Models\Category::select('id', 'template')->get()->toArray();

But this should be valid code

$categories = \App\Models\Category::all(['id', 'template'])->toArray();
Sinnbeck's avatar

Is the code inside a constructor?

public function __construct() 
{
  //
} 

If so it will be run whenever that class is read (never do this)

BigSpender's avatar

nope. this code located inside public method.

Sinnbeck's avatar

Well something must be triggering it. It shouldn't run on composer install. Can you post the whole file?

MichalOravec's avatar

You autoload this path "app/Services" in composer.json.

So remove it from "classmap".

I guess that your classes in app/Services follow PSR-4 so it's not neccesary.

1 like
BigSpender's avatar

We almost near here. Removed from autoload. But some still trigger app/Services

BigSpender's avatar

$ php artisan migrate

Illuminate\Database\QueryException

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'project.categories' doesn't exist (SQL: select `id`, `template` from `categories`)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667▕         // If an exception occurs when attempting to run a query, we'll format the error
    668▕         // message to include the bindings with SQL, which will make this exception a
    669▕         // lot more helpful to the developer instead of just the database's errors.
    670▕         catch (Exception $e) {
  ➜ 671▕             throw new QueryException(
    672▕                 $query, $this->prepareBindings($bindings), $e
    673▕             );
    674▕         }
    675▕ 

  • A table was not found: You might have forgotten to run your migrations. You can run your migrations using `php artisan migrate`. 
    https://laravel.com/docs/master/migrations#running-migrations

  1   [internal]:0
      App\Console\Commands\Parser::__construct(Object(App\Services\Image), Object(App\Services\Category), Object(App\Services\Brand))

      +12 vendor frames 
  14  app/Services/Category.php:46
      Illuminate\Database\Eloquent\Model::all()

still appears. Could it be Commands will be triggered during composer install? One strange moment: it successfully implements on my local machine :)

Sinnbeck's avatar

They shouldn't, unless you have built in something that will yourself. Like in a service provider. But be aware that constructors might be called, triggering some unintended code.

I still believe it is caused by injecting or running something in the constructor

Čamo's avatar

Any progress in this issue? I have the same problem. SQL triggers while composer install. I dont understand why.

BigSpender's avatar

@Čamo , you need to check where it fails. The next step is triggering console command. If console command uses in contructor for instance injections(some model or ..), then it triggers data in tables, that have not created.

Čamo's avatar

As I found out it triggers AppServiceProvider::boot() method (dont understand why on composer install...) and there is a line

	view()->share('currency', app('activeCurrency')->symbol);

How can I remove it from boot() or where to store this call properly?

MichalOravec's avatar
Level 75

Use it like this

use Illuminate\Support\Facades\App;

if (! App::runningInConsole()) {
    view()->share('currency', app('activeCurrency')->symbol);
}
5 likes
krs's avatar

@MichalOravec I too have to reply to that old post - made the same mistake sharing data in the AppServiceProvider withouth checking, if it is running in console. Thanks a lot!

Čamo's avatar

Ok but what if I need to run this code e.g. as cron which sends an emails? This line for views is not the only reason of this error. There are also something like

$settings = GlobalSettings::all();
...
Config::set('mail', $config);

which wants to set config variables...

Please or to participate in this conversation.