【问题标题】:Doctrine\DBAL\Schema\SchemaException: There is no column with name 'deleted_at' on table 'users'Doctrine\DBAL\Schema\SchemaException:表 'users' 上没有名称为 'deleted_at' 的列
【发布时间】:2021-02-05 14:32:20
【问题描述】:

我构建了一个 API(背面:Laravel 和正面:Angular),我想测试我的 API。 我是 Laravel 的初学者,当我想做一些集成测试时遇到了问题。确实,我尝试了这个小测试:

class userTest extends TestCase
{

    public function testExample()
    {
        $response = $this->getJson('/api/listUsers');
        $response->assertStatus(200);
    }

}

我总是有这个错误:

Doctrine\DBAL\Schema\SchemaException: There is no column with name 'deleted_at' on table 'users'.

虽然我的用户表中还存在“deleted_at”,所以我不明白,我在互联网上搜索但在运行 phpunit 命令时没有人收到此错误。

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('firstname');
            $table->string('lastname');
            $table->string('email')->unique();
            $table->string('password');
            $table->date('date_of_birth');
            $table->rememberToken();
            $table->timestamps();
            $table->softDeletes();
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');

        Schema::table('users', function (Blueprint $table) {
            $table->dropSoftDeletes();
        });
    }
}

还有我的用户类:

class User extends Authenticatable implements JWTSubject
{
    // 1. Dépendances
    use SoftDeletes;
    use Notifiable;


    // 2. properties

    protected $appends = ['fullname', 'age'];

    protected $fillable = [
        'firstname', 'lastname', 'email', 'password','date_of_birth'
    ];

    protected $hidden = [
        'password', 'remember_token', 'created_at', 'updated_at', 'deleted_at', 'pivot',
    ];
    protected $table = 'users';




    // 3. getters & setters
    public function setPasswordAttribute($password)
    {
        $this->attributes['password'] = bcrypt($password);
    }

    public function getFullNameAttribute() {
        return ucfirst($this->firstname) . ' ' . ucfirst($this->lastname);
    }

    public function getAgeAttribute()
    {
        return Carbon::parse($this->attributes['date_of_birth'])->age;
    }

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }


    // 4. other methods
    public function projects()
    {
        return $this->belongsToMany(Project::class);
    }

}

我可以帮忙吗??

【问题讨论】:

  • 请分享您的用户类别。
  • 你使用软删除了吗?
  • 是的,我正在使用软删除
  • 您在迁移文件时是否收到此错误?
  • 我分享了我的用户类@El_Vanja

标签: php laravel api testing integration-testing


【解决方案1】:

尝试运行以下命令。

php artisan cache:clear
php artisan config:clear
php artisan config:cache

另外,尝试从 $hidden 属性中删除 deleted_at 列。

【讨论】:

  • 谢谢,我删除了软删除,现在可以使用了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-21
  • 2023-03-20
  • 2016-09-20
  • 2021-05-27
  • 1970-01-01
  • 2015-09-23
  • 2013-05-11
相关资源
最近更新 更多