You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?
Yes, it's still reproducable
What version of Laravel Excel are you using?
3.1.63
What version of Laravel are you using?
11.43.2
What version of PHP are you using?
8.4
Describe your issue
Due to the ->save() call in src/Imports/Persistence/CascadePersistManager.php:44 which happens before the relations are persisted it is not possible to use the PersistRelations interface for relations which are required (NOT NULL).
How can the issue be reproduced?
Create two models with a BelongsTo relation between them
Make the relation ->foreignId(RelatedModel::class)->constrained() (NOT NULL)
Create a ToModel import including $model->setRelation('related', new RelatedModel);
Call import on the importer
What should be the expected behaviour?
I'd expect to either note in the docs that this is a limitation of the interface (a massive one) or that it'd not save the model before persisting the relations.
The text was updated successfully, but these errors were encountered:
If you have a look at the test https://github.com/SpartnerNL/Laravel-Excel/blob/3.1/tests/Concerns/ToModelTest.php#L169 , I'm doing BelongsTo differently than your example, have a look if doing it that way makes any difference. (Perhaps the correct example is missing in the docs then - if so, would appreciate a PR to the docs). If not please provide a failing unit test to the ToModelTest.
@patrickbrouwers Thanks for the response. I tried the method using associate instead of setRelation but it has the same result.
Due to the save() function being called before any relations are persisted, any NOT_NULL FK columns will cause the DB to throw a: Integrity constraint violation: 1048 Column '<col>' cannot be null
I could provide a test, but it'd require a new model + NOT NULL relation in the tests which would affect other tests as well (if the NOT NULL relation is added on an existing model). How would you approach this? Add two new models with different relation?
Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?
What version of Laravel Excel are you using?
3.1.63
What version of Laravel are you using?
11.43.2
What version of PHP are you using?
8.4
Describe your issue
Due to the
->save()
call insrc/Imports/Persistence/CascadePersistManager.php:44
which happens before the relations are persisted it is not possible to use thePersistRelations
interface for relations which are required (NOT NULL).How can the issue be reproduced?
->foreignId(RelatedModel::class)->constrained()
(NOT NULL)ToModel
import including$model->setRelation('related', new RelatedModel);
import
on the importerWhat should be the expected behaviour?
I'd expect to either note in the docs that this is a limitation of the interface (a massive one) or that it'd not save the model before persisting the relations.
The text was updated successfully, but these errors were encountered: