laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.53k stars 11.02k forks source link

QueryException: General error: 1 no such table: {table_name} (SQL: select * from "{table_name}") #25223

Closed dinhquochan closed 6 years ago

dinhquochan commented 6 years ago

Description:

Full error message:

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1 no such table: {table_name} (SQL: select * from "{table_name} ")

Steps To Reproduce:

dd(DB::table('{table_name}')->get());
Illuminate\Support\Collection {#113
  #items: array:1 [
    0 => {... }
  ]
}
staudenmeir commented 6 years ago

AppServiceProvider::boot() is executed before the table is created. Why do you have to access the table so early?

dinhquochan commented 6 years ago

And AppServiceProvider::register(), so why with MySQL Connection working well :(

staudenmeir commented 6 years ago

The table must already exist in the MySQL database. The in-memory SQLite database is always empty.

harshitbatra commented 5 years ago

@dinhquochan I have the same issue. I'm using SQLite :memory: for testing purpose whereas my database is in MySQL and working fine but my test are failing as it says "lluminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: users". How did you fix the issue? Please help @staudenmeir

dinhquochan commented 5 years ago

AppServiceProvider::boot() is executed before the table is created. I don't fix the issue! I just replace a solution.

ShinHyungJune commented 5 years ago

i solved this problem using by "use RefreshDatabase;" in Test file.

wdevmaster commented 5 years ago

I have the same problem executing a test with dusk and even though I have RefreshDatabase implemented in the test I continue with the problem ... could someone help me?

Here is the code of the test: CartTest.php

<?php

namespace Tests\Browser;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;
use Tests\Browser\Pages\CartPage;
use Tests\Browser\Pages\ProductShowPage;
use Tests\DuskTestCase;
use Vanilo\Framework\Models\Product;
use Vanilo\Product\Models\ProductState;

class CartTest extends DuskTestCase
{
    use DatabaseMigrations;

    private $productA;

    private $productB;

    public function setUp() :void
    {
        parent::setUp();

        $this->productA = Product::create([
            'name'  => 'Fiat Punto',
            'sku'   => 'FTA-PUNTO',
            'state' => ProductState::ACTIVE(),
            'price' => 4500
        ]);

        $this->productB = Product::create([
            'name'  => 'Dacia Sandero',
            'sku'   => 'DCA-SANDERO',
            'state' => ProductState::ACTIVE(),
            'price' => 7900
        ]);
    }

    /** @test */
    public function it_can_open_cart()
    {
        $this->browse(function (Browser $browser) {
            $browser
                ->visit(new CartPage())
                ->assertSee('Your cart is empty');
        });
    }

    /** @test */
    public function it_can_use_cart_properly()
    {
        $this->browse(function (Browser $browser) {
            $browser
                ->visit(new ProductShowPage($this->productA))
                ->addToCart()
                ->visit(new ProductShowPage($this->productA))
                ->addToCart()
                ->visit(new ProductShowPage($this->productB))
                ->addToCart()
                ->visit(new CartPage())
                ->assertSourceHas('name="qty" value="1"')
                ->assertSourceHas('name="qty" value="2"')
                ->assertSee($this->productA->name)
                ->assertSee($this->productB->name)
                ->assertSee(format_price($this->productA->price * 2))
                ->assertSee(format_price($this->productB->price))
                ->assertSee(format_price($this->productA->price * 2 + $this->productB->price))
                ->deleteFromCart($this->productA)
                ->assertDontSee($this->productA->name);
        });
    }
}

Pages\ProductShowPage.php

<?php

namespace Tests\Browser\Pages;

use Laravel\Dusk\Browser;
use Laravel\Dusk\Page;
use Vanilo\Framework\Models\Product;

class ProductShowPage extends Page
{
    /** @var Product */
    private $product;

    /**
     * ProductShowPage constructor.
     *
     * @param Product $product
     */
    public function __construct(Product $product)
    {
        $this->product = $product;
    }

    /**
     * Get the URL for the page.
     *
     * @return string
     */
    public function url()
    {
        return route('product.show', $this->product);
    }

    /**
     * Assert that the browser is on the page.
     *
     * @param  Browser $browser
     *
     * @return void
     */
    public function assert(Browser $browser)
    {
        $browser->assertSee($this->product->name);
    }

    public function addToCart(Browser $browser)
    {
        $browser->press("Add to cart");
    }
}
ArmanKamal commented 4 years ago

Instead of using DatabaseMigrations use RefreshDatabase; It will be solved