The data is set, but i get an "Column does not allow nulls" error

Posts   
 
    
Vestas
User
Posts: 6
Joined: 27-Jun-2008
# Posted on: 18-Jun-2009 12:12:56   

Hi

Consider this:

DB: I have a table with a column (varchar) called Address (allow null is false).

Code: I do not set the Address field (it is "") and when I try to save the entity I get an "Column does not allow nulls" error?!

You could argue that an empty string is the same as null. But consider this:

DB: On the same table I have another column (bit) called MemeberOfGameClub (allow null is false).

Code: I set the MemeberOfGameClub to True and save works fine. But when I leave it untouched or set it to false I get the same error ("Column does not allow nulls")

It seems that LLBL tries to save a nothing/null to the database when the data is untouched (not changed) in my code.

A fix could be to setup at default value in the DB and when LLBL tries to save a null then the default value will be used instead. But I do not like that solution - and I am wondering why LLBL converts a “” (or false) to null!??? disappointed

I use LLBL version 2.6 and MS SQL Server 2005.

Her is some real-word info:


Code (save):

adapter.SaveEntityCollection(sitesChanged, True, False)


Exception:
Failed  SaveSiteToVP2009Test    HumanResources.Test Test method HumanResources.Test.PackageManagerTest.SaveSiteToVP2009Test threw exception:  
SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException: An exception was caught during the execution of an action query: 
Cannot insert the value NULL into column 'ISOCountryName', table 'VestasPeople2009.dbo.Site'; column does not allow nulls. INSERT fails.
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Jun-2009 18:37:59   

LLBLGen doesn't manage default values. If you don't set a field, LLBLGen wont send it to the persistence storage.

What you can do is set default values at code yourself overriding OnInitialized (http://llblgen.com/TinyForum/Messages.aspx?ThreadID=14456&StartAtMessage=0&#80655).

David Elizondo | LLBLGen Support Team
Vestas
User
Posts: 6
Joined: 27-Jun-2008
# Posted on: 18-Jun-2009 22:38:20   

Thanks for replying.

If I do set the value. E.g. I set a string to " or a boolean to false I still get the error.

As far as I see it, if you e.g. have this:

  1. A boolean fields that is set to False.
  2. A corresponding database field that does not allow nulls.

... you will get a "Column does not allow nulls" error on save.

(if you i step 1 set it to true everything is ok)

I wonder if there is a way to tell LLBL that the field is dirty (even if the field is "" or false) - and thereby force LLBL to send it to the persistence storage?

BTW: I have the same problem when using Integers. When I set the value to 0 and try to save it I get the "Column does not allow nulls" error.

Please advice how to workaround this "feature" in LLBL :-)

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 19-Jun-2009 05:49:39   

Hi Vestas,

I have default values working great using OnInitialized. Please see thread here:

http://llblgen.com/tinyforum/Messages.aspx?ThreadID=15323

Hope this helps!

Ryan

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 19-Jun-2009 10:09:53   

I wonder if there is a way to tell LLBL that the field is dirty

myEnity.Fields["myField"].IsChanged = true;
Vestas
User
Posts: 6
Joined: 27-Jun-2008
# Posted on: 19-Jun-2009 10:46:15   

Thanks all

Reading Ryan's link and other stuff I found the "OnInitialized" approach to be most acceptable.

Imports SD.LLBLGen.Pro.ORMSupportClasses

Namespace Vestas.HumanResources.Data.EntityClasses
  Partial Public Class SiteEntity

    Protected Overrides Sub OnInitialized()
      MyBase.OnInitialized()
      If Not Me.Fields Is Nothing AndAlso (Me.Fields.State <> EntityState.Fetched) Then

        ' To ensure that LLBL will send these fields to the database, we set default value.
        ' If values are not set then an "Column does not allow nulls" will happen due to the fact that the fields in db does not allow nulls.
        AddressLine_01 = ""
        ISOCountryCode = ""
        ISOCountryName = ""
        EmployeesAllowedHiddenInfo = false

      End If
    End Sub

  End Class
End Namespace

By overriding OnInitialized I do no longer get the error.