【问题标题】:Column does not exist Laravel Factory列不存在 Laravel 工厂
【发布时间】:2020-11-24 12:21:57
【问题描述】:

我有一张桌子

public function up()
{
    Schema::create('parties', function (Blueprint $table) {
        $table->id();
        $table->integer('place_id');
        $table->tinyInteger('status')->default(0);
        $table->dateTime('utc_date');
        $table->dateTime('local_date');
        $table->timestamps();
    });

    Schema::table('parties', function (Blueprint $table) {
        $table->index('place_id');

        $table->foreign('place_id')
            ->references('id')
            ->on('places')
            ->onDelete('restrict');
    });
}

和模型工厂

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Models\Party;
use Faker\Generator as Faker;

$factory->define(Party::class, function (Faker $faker) {
    $date = $faker->dateTimeBetween(now()->subDays(3), now());

    return [
        'place_id' => $faker->numberBetween(1, 9),
        'status' => $faker->numberBetween(0, 3),
        'utc_date' => $date,
        'local_date' => \Carbon\Carbon::make($date)->addHours(3),
    ];
});

如果我在 HomeController 中调用 create 方法,它可以正常工作

    factory(Party::class, 10)->create();

但是当我在测试中调用它时出现错误

SQLSTATE[42703]:未定义列:7 错误:列“utc_date” 关系“当事人”不存在第 1 行:插入“当事人” (“place_id”、“status”、“utc_date”、“lo... ^ (SQL: 插入 "party" ("place_id", "status", "utc_date", "local_date", "updated_at", "created_at") 值 (5, 3, 2020-08-04 00:34:52, 2020-08-04 03:34:52, 2020-08-04 11:42:18, 2020-08-04 11:42:18) 返回“id”)

<?php

namespace Tests\Feature\API;

use App\User;
use Tests\TestCase;
use Laravel\Sanctum\Sanctum;
use App\Models\{Party, Place};
use Illuminate\Foundation\Testing\RefreshDatabase;

class PartiesControllerTest extends TestCase
{
    use RefreshDatabase;

    private User $user;

    protected function setUp(): void
    {
        parent::setUp();
        $this->user =  factory(User::class)->create();
    }

    public function test_get_parties()
    {
        factory(Party::class)->create([
            'place_id' => factory(Place::class)->create()->id,
        ]);

        dd(Party::all());
        Sanctum::actingAs($this->user);
    }
}

【问题讨论】:

  • 在你的派对模型中,确保你有:protected $table = 'parties';
  • 我有。正如我上面所说,如果我在 HomeController.php 中调用 factory(Party::class) 的方法,它的工作完全正确
  • 你在使用 PostgreSQL 吗?
  • 是的。我正在使用 postgres
  • @DimitriMostrey 您可以在官方 php 文档中找到有关分组命名空间的信息。这是php7的一个新特性php.net/manual/en/…

标签: sql laravel postgresql laravel-7


【解决方案1】:

请确保模型中的可填充数组有这个键,

并尝试使用碳代替带有日期的faker

(Carbon::now())->subDays(3);

【讨论】:

    【解决方案2】:

    尝试运行php artisan migrate:fresh。如果您使用另一个数据库进行测试并使用另一个.env 文件,请尝试运行php artisan migrate:fresh --env=testing。 例如:您可以在此文件中使用文件env.testingAPP_ENV=testing

    也许你后来添加了这个字段并且没有刷新数据库。另外,检查该字段是否存在于可视数据库中。 如果没有帮助,请尝试运行 composer dump-autoload 并重试。

    【讨论】:

      【解决方案3】:

      我正在使用第二个数据库进行测试。我连接到这个db,发现这个表还没有刷新。我不知道为什么,因为我使用了 RefreshDatabase 特征。我手动添加了这个字段来解决这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-20
        • 1970-01-01
        • 1970-01-01
        • 2019-10-07
        • 2017-05-29
        • 2021-01-26
        • 2023-02-04
        • 2020-01-22
        相关资源
        最近更新 更多