HasMany().Not.KeyNullable() uni-directional mapping generates NULLable FK DDL
Hello,
It seems like HasMany().Not.KeyNullable() not quite properly generates DDL with SchemaExport. Assuming it should output NOT NULL FK column on the child side. However what I get is NULLable FK column.
HasMany(x => x.Items)
.Not.Inverse()
.Not.KeyNullable()
.Not.KeyUpdate()
.Cascade.AllDeleteOrphan();
The background of such modelling is the DDD related motivations where the AggregateRoot is managing its child entities, but children are not holding back reference to the AggregateRoot.
Am I doing something wrong or is this not working as I expect it to?
Cheers
I have exactly the same need, and exactly the same problem. In my project, we also have an AggregateRoot that is managing a collection of child entities, also using composition, so deleting orphans is also a must.
It is not a problem to alter the database script by hand, since we do not rely on automatic schema updates, and instead roll out hand-written migration scripts.
However, it would be nice to see that HNIbernate respects the mapping configuration, and generates proper schema. Otherwise, how can we be sure that it won't try to do insert null and then subsequent update to a value during session flush at runtime?
What's the state of this?
@rgomez90 as far as I know, no one work on this. You are more than welcome to submit a PR. See contributing.
@hazzik thank you. Will take a look at it in the weekend.
@rgomez90 ,
Did u attempt to fix this bug yet or did you blocked?
Hey,
looking at the generated mapping xml fluent nhibernate generated i see this
<bag access="nosetter.camelcase" cascade="all-delete-orphan" name="XXX">
<key not-null="true" update="false">
<column name="LineItem_id" />
</key>
<one-to-many class="XXX.Domain.XXX, XXX.Domain, Version=67.2017.9.1, Culture=neutral, PublicKeyToken=null" />
</bag>
In the documentation here ( https://nhibernate.info/doc/nhibernate-reference/collections.html#collections-example) it show what the xml mapping should look like if you want the FK to be not nullable
<set name="Children">
<key column="parent_id" not-null="true"/>
<one-to-many class="Child"/>
</set>
Notice the different in the column attribute vs the column node. Are the both the same and valid mappings?
Notice the different in the column attribute vs the column node. Are the both the same and valid mappings?
yes