FilamentF
Filament14mo ago
Schilly

Import Action - "unknown column '' in `where clause` "

Hello - I am trying to get this importer to behave... I am using it to import records into a Relationship Pivot Table.

The relationship is a belongsToMany relationship - and knowing that it will not play nice on that end, I have made up a Pivot Model to interact with. Here is the pivot model:

<?php

namespace App\Models\S3Operations;

use App\Models\S3Operations\Operation;
use App\Models\S5Logistics\Warehouse;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\Pivot;

class OperationWarehouse extends Pivot
{

    protected $fillable =
    [
        'operation_id',
        'warehouse_id',
        'fuel',
        'aircraft',
        'equipment'
    ];

    public function operation(): BelongsTo
    {
        return $this->belongsTo(Operation::class);
    }

    public function warehouse(): BelongsTo
    {
        return $this->belongsTo(Warehouse::class);
    }
}
To go along with the pivot model - I ahve made up the Importer, as follows:
<?php

namespace App\Filament\Imports;

use App\Models\S3Operations\OperationWarehouse;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;

class OperationWarehouseImporter extends Importer
{
    protected static ?string $model = OperationWarehouse::class;

    public static function getColumns(): array
    {
        return [
            ImportColumn::make('operation_id')
                ->integer()
                ->requiredMapping(),
            ImportColumn::make('warehouse_id')
                ->integer()
                ->requiredMapping(),
            ImportColumn::make('fuel')
                ->numeric(decimalPlaces: 2)
                ->requiredMapping(),
            ImportColumn::make('aircraft')
                ->requiredMapping(),
            ImportColumn::make('equipment')
                ->requiredMapping(),
        ];
    }

    public function resolveRecord(): ?OperationWarehouse
    {
        $warehouseID = $this->data['warehouse_id'];

        return OperationWarehouse::firstOrNew(["warehouse_id" => $warehouseID]);

        // return new OperationWarehouse();
    }

    public static function getCompletedNotificationBody(Import $import): string
    {
        $body = 'Your equipment import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.';

        if ($failedRowsCount = $import->getFailedRowsCount()) {
            $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.';
        }

        return $body;
    }
}
The problem I am having is - the importer is throwing an error as follows:
[2024-11-01 15:33:37] local.ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (Connection: mysql, SQL: update `operation_warehouse` set `fuel` = 750, `aircraft` = [{"airframe_id":"4","qty":"22"},{"airframe_id":"8","qty":"5"}], `equipment` = [{"equipment_id":"177","qty":"150"},{"equipment_id":"6","qty":"150"},{"equipment_id":"30","qty":"150"}], `operation_warehouse`.`updated_at` = 2024-11-01 15:33:37 where `` = 1 and `` = 1) {"userId":205358980568973312,"exception":"[object] (Illuminate\\Database\\QueryException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (Connection: mysql, SQL: update `operation_warehouse` set `fuel` = 750, `aircraft` = [{\"airframe_id\":\"4\",\"qty\":\"22\"},{\"airframe_id\":\"8\",\"qty\":\"5\"}], `equipment` = [{\"equipment_id\":\"177\",\"qty\":\"150\"},{\"equipment_id\":\"6\",\"qty\":\"150\"},{\"equipment_id\":\"30\",\"qty\":\"150\"}], `operation_warehouse`.`updated_at` = 2024-11-01 15:33:37 where `` = 1 and `` = 1) at C:\\Users\\patri\\Herd\\hvy\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:825)
But the "warehouse_id" is 100% being set in the firstOrNew method... Thoughts?
Was this page helpful?