tills98
1 week ago

General error: 1 no such table: main.id (SQL: insert into "sessions"...)

Posted 1 week ago by tills98

I've got a very scary exception on my unit test for a simple insert by a simple factory

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1 no such table: main.id (SQL: insert into "sessions" ("title", "order", "start_screen", "overview_screen", "journey_id", "updated_at", "created_at") values (vero, 8, nostrum, reiciendis, 1, 2020-03-26 19:58:35, 2020-03-26 19:58:35))

My unit test:

class SessionTest extends TestCase
{
    use DatabaseMigrations, RefreshDatabase;

    /**
     * A basic unit test example.
     *
     * @return void
     */
    public function testSession()
    {
        /** @var Journey $journey */
        $journey = factory(Journey::class)->create();
        /** @var Session $session */
        $session = factory(Session::class)->create([ 'journey_id' => $journey->id ]);

        $this->assertTrue(true);
    }
}

My Session model

class Session extends Model
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public $fillable = [
        'title',
        'order',
        'start_screen',
        'overview_screen'
    ];

    /**
     * Journey of session
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function journey() {
        return $this->belongsTo(Journey::class, 'journeys');
    }
}

My Session factory

$factory->define(\App\Session::class, function (Faker $faker) {
    return [
        'title' => $faker->word,
        'order' => $faker->numberBetween(0, 10),
        'start_screen' => $faker->word,
        'overview_screen' => $faker->word,
    ];
});

My sessions schema:

class CreateSessionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('sessions', function (Blueprint $table) {
            $table->integer('id')->autoIncrement();
            $table->integer('journey_id')->unsigned();
            $table->string('title');
            $table->integer('order')->unsigned();
            $table->integer('start_screen')->nullable();
            $table->integer('overview_screen')->nullable();
            $table->timestamps();

            $table->foreign('journey_id')->references('journeys')->on('id');
        });
}

The factory for my model Journey works perfectly.

I don't know why this factory is searching for table "main". Do you know what this means?

Please sign in or create an account to participate in this conversation.