mardi 14 juillet 2015

Fluent Nhibernate creates two foreign key constraints for the same relation

I have a primitive parent-child relationship between two classes mapped with Fluent NHibernate. The mapping looks like this:

Table("Parent");
Id(parent => parent.Id).GeneratedBy.Native();
Map(parent => parent.Identifier).Unique().Index("ix_Identifier");
HasMany(parent => parent.Children).KeyColumn("ParentIdentifier").Inverse();

Table("Child");
Id(child => child.Id).GeneratedBy.Native();
References(child => child.Parent, "ParentIdentifier").PropertyRef(parent => parent.Identifier);

I also make use of the SchemaExport feature to create the schema during runtime. Crazily this generates two foreign key constraints for the same relation (note the Inverse() for the HasMany side), which causes any INSERT to fail with a foreign key violation. Fluent NHibernate generates the following schema SQL:

CREATE TABLE Child (
    Id  integer primary key autoincrement,
    ParentIdentifier TEXT,
    constraint FK652F119EB674D0AD foreign key (ParentIdentifier) references Parent (Identifier),
    constraint FK652F119EB674D0AD foreign key (ParentIdentifier) references Parent
)

I believe that the second constraint is wrong as it references Parent by its primary key, which is not the same column I want to reference. The former constraint seems to be correct.

Also, when applying .ForeignKeyConstraintName("none") to the HasMany side the latter constraint is not generated at all and everything is fine. But this seems like a workaround for a misconfiguration of mine.

What am I doing wrong here?

Aucun commentaire:

Enregistrer un commentaire