Tuesday, 26 August 2008

HasMany and null foreign key columns

I’ve been trying to figure out why our data layer wasn’t saving a new item that we’d added to a [HasMany] collection.

To figure out what was happening, I build a simple sample based on the examples from the Castle ActiveRecord documentation.

ClassDiagram1

This generates a database schema like this:

BlogPostTableDiagram

Normally you’d make the foreign key column ‘blogid’ not null, but (as mentioned in this thread) for NHibernate, it does an INSERT then an UPDATE so the foreign key column does need to be nullable.

I can understand this might be the case if the Blog entity hadn’t been saved, but it is surprising that it doesn’t recognise that it could use a single INSERT statement if it does know the foreign key value.

No comments: