jgravois's avatar

No Such Table

I use an sqlite database in memory for testing (set in my phpunit.xml file).

I verify that I have a migration for my invoices table.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateInvoicesTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('invoices')) { return; }
        
        Schema::table('invoices', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('consignment_code')->nullable();
            $table->string('route_code')->nullable();
            $table->string('route_description')->nullable();
            $table->string('part_number')->nullable();
            $table->string('serial_no')->nullable();
            $table->string('description')->nullable();
            $table->string('invc_number')->nullable();
            $table->string('so_number')->nullable();
            $table->string('cond')->nullable();
            $table->string('company')->nullable();
            $table->string('salesperson')->nullable();
            $table->date('invoice_date')->nullable();
            $table->double('price', 10, 2)->nullable();
            $table->double('cost', 10, 2)->nullable();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::table('invoices', function (Blueprint $table) {
            //
        });
    }
}

I have an Invoices factory

<?php

/** @var Factory $factory */

use App\Models\Invoice;
use Carbon\Carbon;
use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factory;

$factory->define(Invoice::class, function (Faker $faker) {
    return [
        'consignment_code' => $faker->word,
        'invc_number' => $faker->word,
        'route_code' => $faker->word,
        'route_description' => $faker->word,
        'part_number' => $faker->word,
        'serial_no' => $faker->word,
        'description' => $faker->word,
        'cond' => 'AR',
        'company' => 'ToysRUs',
        'salesperson' => 'JGRAV',
        'invoice_date' => Carbon::yesterday(),
        'ship_date' => Carbon::today(),
        'gross' => 1200,
        'qty' => 1,
        'cost' => 200,
        'net' => 1000,
    ];
});

So I am writing my first "sanity" test

<?php

namespace Tests\Feature;

use App\Models\Invoice;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Tests\UamTestCase;

class InvoicesControllerTest extends UamTestCase
{
    /** @test */
    public function factory_makes_an_invoice () {
        //$this->withoutExceptionHandling();

        $invc = factory(Invoice::class)->create();

        $this->assertEquals(1000, $invc->net);
    } // end test
}

I run the test and get this error which I have never seen before and am not sure how to fix

Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1 no such table: invoices (SQL: alter table "invoices" add column "id" integer not null primary key autoincrement)
0 likes
2 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

It should be ::create not ::table (table modifies existing)

The table method on the Schema facade may be used to update existing tables.

To create a new database table, use the create method on the Schema facade.

Schema::create('invoices', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('consignment_code')->nullable();
            $table->string('route_code')->nullable();
            $table->string('route_description')->nullable();
            $table->string('part_number')->nullable();
            $table->string('serial_no')->nullable();
            $table->string('description')->nullable();
            $table->string('invc_number')->nullable();
            $table->string('so_number')->nullable();
            $table->string('cond')->nullable();
            $table->string('company')->nullable();
            $table->string('salesperson')->nullable();
            $table->date('invoice_date')->nullable();
            $table->double('price', 10, 2)->nullable();
            $table->double('cost', 10, 2)->nullable();
            $table->timestamps();
        });
1 like

Please or to participate in this conversation.