馃悰 Self transation cannot be resolved.聽#489
Copy link
Copy link
Closed
Labels
type:bugBugBug
Description
No duplicates 馃ゲ.
- I have searched for a similar issue in our bug tracker and didn't find any solutions.
What happened?
Hi,
I am getting a following error when persisting a self referencing entitiy, such as following:
#[Entity(database: 'main')]
class Catalogue
{
#[Column(type: 'primary')]
private int $id;
#[RefersTo(target: self::class, nullable: true)]
private ?self $catalogue = null;
#[HasMany(target: self::class)]
private array $catalogues = [];
}Error message:
Cycle\ORM\Exception\TransactionException: Transaction can't be finished. Some relations can't be resolved:
Create new `catalogue`
- catalogue.catalogues:catalogue (Cycle\ORM\Relation\ShadowBelongsTo)
It seems that the ORM is having trouble understanding the selfReferencing relation.
Version
- ORM 2.8
- PHP 8.3.7Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
type:bugBugBug
Type
Projects
- StatusShow more project fieldsReleased
Activity
roxblnfk commentedon Jun 23, 2024
There is test for many-to-many relation
https://github.com/cycle/orm/tree/many-to-many-self-cyclic
peldax commentedon Jun 24, 2024
Hi @roxblnfk,
I found that when the
HasManycataloguesrelation is marked asnullable, the persisting happens correctly.Does it make sense that the HasMany relation must explicitly know whether the targeting entity allows null or not?
In my opinion the
BelongsTo/RefersTorelations should have the final (and possibly the only) word about its nullability - this is the relevant column after all.roxblnfk commentedon Jun 25, 2024
Hi
At the ORM level, we declare relationships, not table columns.
For example, there can be a situation:
Parent#hasOne(Child, nullable=true)+Child::belongsTo(Parent, nullable=false). Here, theParentmay not have aChildbecause the relationship is optional in this direction. However, if aChildappears in the database, it will always have aParent.HasOnedoes not affect thenullableproperty of theparent_idcolumn.However, it seems odd that the
nullableproperty inHasManyaffects anything. Could you add a test case so that we can poke around with it?peldax commentedon Jun 25, 2024
Hi,
Thanks for the reply.
I understand the usecase with HasOne, but this does not apply on the HasMany relation - there either exists some related entities or not. I think that the nullability of a target is irrelevant on the HasMany relation, please correct me if I am wrong.
Either way, it is great that it is not an intended behaviour and it should not change anything in the persisting process. I will dig deeper and try to provide a solution. I will start with the testcase.