Filament ImportAction: skipping duplicate keys without aborting the whole import
0 reputation · 21 Jan 2026, 18:54 UTC
0 reputation · 21 Jan 2026, 18:54 UTC
I'm using Filament's built-in ImportAction (Filament v3) to restore records from a CSV export, mapping columns to model attributes through the standard column-mapping closures. The target table has a unique key that some rows in the file will violate, because a subset of the records already exists in the database.
From what I can tell, the import runs inside a single database transaction, so when the database raises a duplicate-key exception the entire job fails and every previously imported row is rolled back. For a large restore file that means one conflicting row discards thousands of valid ones. I also see a shouldImportRow-style per-row callback mentioned in the docs, but I can't find clear guidance on whether skipping a row there is the intended way to tolerate duplicates, or how it interacts with the transaction boundary and the failure reporting.
My constraints: I need a partial import (skip conflicting rows, keep the rest), and I want a reliable way afterward to verify which rows were actually persisted versus skipped.
Questions:
35275 reputation · 21 Jan 2026, 19:34 UTC
Filament’s ImportAction executes the entire CSV import inside a single database transaction. If any row triggers a duplicate‑key exception, the transaction is rolled back, discarding all previously inserted rows.
The shouldImportRow callback runs for each row before the insert attempt. Returning false skips that row and prevents the insert, so no duplicate‑key exception is raised for that row. However, the callback still runs inside the same transaction; a later row that does cause a duplicate‑key error will still abort the whole transaction.
To achieve a partial import you must prevent the duplicate‑key exception from ever occurring. That means detecting existing rows yourself inside shouldImportRow and skipping them before the insert is attempted.
email or a composite of user_id + slug).ImportAction definition, implement shouldImportRow to query the model for an existing record using those columns. Return false when a match is found.afterSaved or import completion hook to collect the IDs of rows that were actually persisted.To confirm that the duplicate‑check in shouldImportRow will work reliably, please specify which column(s) constitute the unique constraint you are trying to avoid violating.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.