ContentPartVersionRecord - Columns must be nullable? Possible Bug?

Topics: Core, Troubleshooting, Writing modules
Aug 18, 2014 at 10:12 PM

Given a part record inheriting ContentPartVersionRecord (instead of ContentPartRecord), I've noticed that once the DB has been migrated, creation of any Content Types which have this part throws an error "Column X does not allow NULL" where X is the name of the property in the Part record class.

The properties are defined as not nullable by the migration class, yet nhibernate seems to think they are, but only when the ContentPartVersionRecord base is used.

I am also calling .ContentPartVersionRecord() in the migration class for the part record definition.

Interestingly, if I manually edit the table via SSMS and set the columns to nullable and replay my code, all the records are committed and everything works.

Anyone else experiencing this strange behaviour?
Aug 19, 2014 at 6:04 PM
Could it be that you updated your migration code without adding a new step? If you blow away your database and go through setup, do you still see the issue?
Aug 19, 2014 at 8:07 PM
Yes, I reverted my db prior to the migration version and the exact same thing happened. I thought too that it was something I did between migrations.

Also I forgot to add I'm attaching the part to a content type which is draftable, does that make any difference?

Soon as this line of code runs I get the exception under debug:

Dim item = Services.ContentManager.Create("MyDraftableContentType", versionoptions.draft)

"column statusid does not allow NULL"
Aug 20, 2014 at 1:10 AM
The Create method you are showing seems to be invalid - you need to specify a Content Part type as the generic type argument.
Could you show us the code for the part record, the part, the migration and the actual code you use to create a content item of your content type?
Orchard seems to be inserting a record in your table without actually providing values.
It makes sense that it would work when you manually set to columns to be nullable. Perhaps all you need to do is update your migration by specifying a default value for your non-nullable columns.
Aug 20, 2014 at 9:49 AM
Ah, should I be using the "New" method on the ContentManager instead of Create?

For example

Dim ContentItem = Services.ContentManager.New("MyDraftableContentType")
'//set part properties on my versionable part

My understanding is that Create is then not required to be called, as the Content Type will be created automatically at the end of the session.

Then, to create subsequent draft versions I would do:

Dim ContentItem = Services.ContentManager.Get(idOfTheContentItemAbove, VersionOptions.Draft)
'//set part properties on my versionable part

Am I doing it correctly?
Aug 20, 2014 at 9:55 PM
Okay, I've managed to get this working now:

Changing this:

Dim ContentItem = Services.ContentManager.Create("MyDraftableContentType", VersionOptions.Draft)

ContentItem.As(of MyVersionablePart)().SomeProperty = 123

To This:

Dim ContentItem = Services.ContentManager.New("MyDraftableContentType") MyVerisonablePart)().SomeProperty = 123

Services.ContentManager.Create(ContentItem, VersionOptions.Draft)
Aug 24, 2014 at 4:41 PM
You should be able to use Create directly, as it internally uses New already. So this should work for creating a new content item:
var contentItem = _contentManager.Create("MyContentType", VersionOptions.Draft);
Then to update that content item and create subsequent versions, you can do this:
var contentItem = _contentManager.Get(contentItemId, VersionOptions.DraftRequired); // The DraftRequired option will create a new version record.
// Update part properties etc.

// Optionally publish your draft: