Filament's Export function creating duplicates.
TL;DR : Filament's export action creating duplicates when exporting larger record sets.
I have a very simple model, resource, and exporter. However when I try to export any more than a hundred or so, it mangles the data and returns duplicate records.
I am using :
laravel/framework: ^10.10
filament/filament: ^3.1 (3.3.20)
Originally I was using pxlrbt/filament-excel and that was doing the same thing, so I converted over to using the inbuilt. Which is where I realised the inbuilt one is also doing the same thing.
Model:
Migration:
I'll add the resource and exporter as comments.
Solution:Jump to solution
Updating here as well. If I am using Dennis' plugin I can just do this and it fixes the duplication.
```swift
ExportBulkAction::make()
->exports([...
25 Replies
Resource:
Exporter:
Note I have tried:
- blowing away the DB and using dummy data and it still generated duplicates.
- adding protected static ?string $primaryKey = 'id'; still generated duplicates
- Using both core and pxlrbt/filament-excel exporters: still generated duplicates
- reverting the code back to about a year ago. still generated duplicates.
- exporting only a few records (like 50) no duplicates.
I guess it must have something to do with pagination. Probably duplicate start date? Can you try adding
id
as a second sorting param?I wonder if it's down to Daylight savings
But you be right about the pagination and duplicate values making it sad. I:
1. converted the date() field to a timestamp
2. regenerated 1000 records
3. tested some exports with various sorting
So far... no duplicates.
But when I sort on another field that has to have duplicate values.... it exports duplicate records.
Sorry lots of edits... so a partial solution for the date but still a bug when sorting by other fields it generates duplicates in the exports.
I couldnt work out how to "Can you try adding id as a second sorting param?" how do I do that sorry?
I have created a demo project to test the bug if you like.
https://github.com/uqjohart/duplication-bug-filament
(While it has sail in it, you don't need sail, and just just deploy as normal)
I included a db dump in the db-dump folder (all dummy data) filament use is
test@test.com
password
To replicate:
1. sot by date that is now random timestamps -> export -> no duplicates
2. sort by name that WILL contain duplicates -> export -> duplicates
Optional:
Switch out the default filament exporter to @Dennis Koch 's export and the same behavior persists.
I might have to submit this as a proper filament bug as well if we don't have an thoughts. My clients are jumping on my back about it :S
GitHub
GitHub - uqjohart/duplication-bug-filament
Contribute to uqjohart/duplication-bug-filament development by creating an account on GitHub.
I should of named that better >_>
GitHub
Exporter exporting incorrectly and causing duplicates with non uniq...
Package filament/filament Package Version v3.3.21 Laravel Version ^12.0 Livewire Version ^3.6 PHP Version 8.3.21 Problem description When exporting a large volume of record (>100/200) the export...
Can you check this? https://github.com/filamentphp/filament/pull/16552
GitHub
Add orderBy qualified keyname for stable pagination sort by barryvd...
Makes sure to add a unique key to the orderBy to prevent missing/duplicate items when pagination because of unstable sorting, when keys are the same.
Description
This adds a default sort to pagina...
Not sure if that's the best solution though
I'll take a look first thing when I get to work in the morning. Appreciate y'all looking into the issue.
for me it was with pagination though, not sure if it uses the same pagination
I made the change to CanPaginateRecords but it doesnt seem to fix it in my test one :c
Hmm... that being said you may have put me on to a fix (And I think it was what @Dennis Koch was telling me to do).
I changed the factory to:
And the resource table column to:
And that seems to have killed the duplicates. Including using pxlrbt\FilamentExcel
It is a PITA to go through all my resources to update, but if it work I am happy until it gets resolved in core.
I need to test on my larger project.
Works on direct columns... but not relational columns such as activityType.name
Especially when activityType.name can be null. Then it is al sorts of messed up.
I don’t think this one is a core issue. If you have an order on a date and you have multiple records with the same date, there is no guarantee that they are sorted the same every time.
Problem is it happens on every field (not just date) that can be non unique :S
My fix doesnt work on relational data such as the activityType.name... if I can get that working I'll be golden.
Seeing as it is breaking on that little clean install app, I cant see it be anything other than being in core as that one has nothing from my larger app other than the simple activity resource and bits.
This bug is seriously making me regret my life choices.
It's not a Core issue. It's a database limitation.
Maybe the export is not a core issue but for the Table component, Filament should handle that
Imho
What's your recommendation? Always put a sort by
id
on the table?GitHub
Exporter exporting incorrectly and causing duplicates with non uniq...
Package filament/filament Package Version v3.3.21 Laravel Version ^12.0 Livewire Version ^3.6 PHP Version 8.3.21 Problem description When exporting a large volume of record (>100/200) the export...
I think Dan said he did something similar
In v4
But for v3 something like this perhaps https://github.com/filamentphp/filament/pull/16556
GitHub
Make stablesort configurable by barryvdh · Pull Request #16556 · ...
Makes sure to add a unique key to the orderBy to prevent missing/duplicate items when pagination because of unstable sorting, when keys are the same.
Description
Re-does #16552
This adds a stableS...
If you need it to be optional
I see you already dived deeper into that.
Solution
Updating here as well. If I am using Dennis' plugin I can just do this and it fixes the duplication.
Seems so stupid obvious now in hindsight. I will take my award for being a spoon now please.
Here you go 🥄
Thank you thank you. I'd like to thank my parents, without whom I would never be able to have reached this level of spoonery.