fluent-nhibernate icon indicating copy to clipboard operation
fluent-nhibernate copied to clipboard

HasMany().Not.KeyNullable() uni-directional mapping generates NULLable FK DDL

Open ghost opened this issue 7 years ago • 10 comments

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

ghost avatar Jun 25 '18 13:06 ghost

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?

oleh-zheleznyak avatar Oct 01 '18 14:10 oleh-zheleznyak

What's the state of this?

rgomez90 avatar Nov 16 '19 01:11 rgomez90

@rgomez90 as far as I know, no one work on this. You are more than welcome to submit a PR. See contributing.

hazzik avatar Sep 18 '20 11:09 hazzik

@hazzik thank you. Will take a look at it in the weekend.

rgomez90 avatar Sep 18 '20 15:09 rgomez90

@rgomez90 ,

Did u attempt to fix this bug yet or did you blocked?

TraGicCode avatar May 26 '21 13:05 TraGicCode

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?

TraGicCode avatar May 26 '21 14:05 TraGicCode

Notice the different in the column attribute vs the column node. Are the both the same and valid mappings?

yes

hazzik avatar Jun 14 '21 08:06 hazzik