F
Filament5mo ago
ericmp

Error when importing

related question: https://discord.com/channels/883083792112300104/1245632711922815066 i upload the csv and all the rows fail. not sure what im missing csv:
name,surname,email,company
eric,mp,[email protected],Test company
name,surname,email,company
eric,mp,[email protected],Test company
local.ERROR: SQLSTATE[22003]: Numeric value out of range: 1264 Out of range value for column 'attempts' at row 1 (Connection: mysql, SQL: update `jobs` set `reserved_at` = 1717144796, `attempts` = 256 where `id` = 256) {"userId":1,"exception":"[object] (Illuminate\\Database\\QueryException(code: 22003)
local.ERROR: SQLSTATE[22003]: Numeric value out of range: 1264 Out of range value for column 'attempts' at row 1 (Connection: mysql, SQL: update `jobs` set `reserved_at` = 1717144796, `attempts` = 256 where `id` = 256) {"userId":1,"exception":"[object] (Illuminate\\Database\\QueryException(code: 22003)
Tables\Actions\ImportAction::make('importContacts')
->importer(ContactsImporter::class)
->options([
'ownerRecordId' => $this->getOwnerRecord()->id
]),
Tables\Actions\ImportAction::make('importContacts')
->importer(ContactsImporter::class)
->options([
'ownerRecordId' => $this->getOwnerRecord()->id
]),
<?php

namespace App\Filament\Imports;

use App\Models\Contact;
use App\Models\EventContact;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;

class ContactsImporter extends Importer
{
protected static ?string $model = Contact::class;

public static function getColumns(): array
{
return [
ImportColumn::make('name')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('surname')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('email')
->requiredMapping()
->rules(['required', 'max:255', 'email']),
ImportColumn::make('company')
->requiredMapping()
->rules(['required', 'max:255']),
];
}

public function resolveRecord(): ?Contact
{
$contact = Contact::firstOrNew([
'email' => $this->data['email'],
]);

EventContact::updateOrCreate([
'contact_id' => $contact->id,
'event_id' => $this->options['ownerRecordId'],
]);

return $contact;
}
}
<?php

namespace App\Filament\Imports;

use App\Models\Contact;
use App\Models\EventContact;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;

class ContactsImporter extends Importer
{
protected static ?string $model = Contact::class;

public static function getColumns(): array
{
return [
ImportColumn::make('name')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('surname')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('email')
->requiredMapping()
->rules(['required', 'max:255', 'email']),
ImportColumn::make('company')
->requiredMapping()
->rules(['required', 'max:255']),
];
}

public function resolveRecord(): ?Contact
{
$contact = Contact::firstOrNew([
'email' => $this->data['email'],
]);

EventContact::updateOrCreate([
'contact_id' => $contact->id,
'event_id' => $this->options['ownerRecordId'],
]);

return $contact;
}
}
5 Replies
ericmp
ericmp5mo ago
didnt remember that firstOrNew does not store the model so $contact->id may be null now i added the $contact->save() after the firstOrNew anyways this doesnt solve it since now i have to add the other columns, since they arent nullable (name, surname, company) not sure if it's the appropiate way to achieve what im trying to achieve (im trying to link event & contact models by the pivot)
toeknee
toeknee5mo ago
public function resolveRecord(): ?Contact
{
$contact = Contact::where('email' , '=', $this->data['email'])->first();
if(!$contact) $contact = Contact::create($this->data);

EventContact::updateOrCreate([
'contact_id' => $contact->id,
'event_id' => $this->options['ownerRecordId'],
]);

return $contact;
}
public function resolveRecord(): ?Contact
{
$contact = Contact::where('email' , '=', $this->data['email'])->first();
if(!$contact) $contact = Contact::create($this->data);

EventContact::updateOrCreate([
'contact_id' => $contact->id,
'event_id' => $this->options['ownerRecordId'],
]);

return $contact;
}
ericmp
ericmp5mo ago
yeah that may be a way, now i have it like this, which i guess is similar:
public function resolveRecord(): Contact
{
$contact = Contact::updateOrCreate([
'email' => $this->data['email'],
], $this->data);

EventContact::updateOrCreate([
'contact_id' => $contact->id,
'event_id' => $this->options['ownerRecordId'],
]);

return $contact;
}
public function resolveRecord(): Contact
{
$contact = Contact::updateOrCreate([
'email' => $this->data['email'],
], $this->data);

EventContact::updateOrCreate([
'contact_id' => $contact->id,
'event_id' => $this->options['ownerRecordId'],
]);

return $contact;
}
the thing i dont understand is this error Numeric value out of range: 1264 Out of range value for column 'attempts' at row 1 but now seems it works not sure if that error can happen again, seems related to the jobs model
toeknee
toeknee5mo ago
Your update or create works too. You have a column called attempts, which you are likely setting a bad default value.
ericmp
ericmp5mo ago
well yeah the jobs table has an attempts field, havent modified the table seems that the error related to the jobs model is not happening anymore as the resolveRecord function is fixed now
Want results from more Discord servers?
Add your server