Null values in Database

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
11/10/2008 10:36:00 PM
Gravatar
Total Posts 6

Null values in Database

Hi.  Love mojoportal so far.  I am developing a module that uses database tables with NULL unqiueIdentifiers in some of the foreign keys.  In other words, sometimes those foreign keys have a value, and some times they are null.  I am using the provided CodeSmith templates to build my data layer classes, and business classes but I notice that these do not create nullable values such as Giud? or int?.   Is there is a reason you do not use nullable values?  If so, how should I handle a foreign key that is sometimes null?  Default values would mess up referential integrity (MSSQL) I think.

Marco

11/11/2008 3:57:34 AM
Gravatar
Total Posts 18439

Re: Null values in Database

I generally don't use nullable foriegn keys. If its a guid column I use empty guid 00000000-0000-0000-0000-000000000000, and if its int I use -1. In both cases if its possible that I will have empty guid or -1 as the value in the column, I don't make the cloumn a foriegn key I just index it. If a row can live without an actual foriegn key value I don't think of it as a foriegn key, to me a true foriegn key must have a value from the corresponding table otherwise I question why to make it a foriegn key. Of course reasonable people may disagree and I won't say you are wrong to disagree, thats just how I look at it.

One of the tricky things for me since mojoportal supports a lot of databases is not all of them natively support guid columns. So even though MS SQL has a newid() function you can use as a default value generator for guid primary keys, I always pass in guids from .NET code instead. For MS SQL tables I often still add the newid as default value on the primary key column, but it doesn't really use it since I pass in the guid in all cases.

So in some dbs I'm using char(36) for guid columns and I must parse it correctly as a guid no matter which db platform is invloved so I don't like any null values, I need empty guid there to make my life easier supporting other db platforms.

If you are building only for MS SQL you can do as you wish, nothing compels you to follow my examples or approaches. The Codesmith templates are only to make a beginning, they don't do everything, they just give a jump start. Feel free to rename and modify any of my Codesmith templates to better suit your preferences. If you add any useful stuff you could also contribute new templates or improvements back to the project.

Best,

Joe

11/22/2008 3:00:48 PM
Gravatar
Total Posts 6

Re: Null values in Database

Thanks for the complete response.  I will at first take the approaches you take, and then once I'm comfortable with the environment, will add my own ideas...

Marco

You must sign in to post in the forums. This thread is closed to new posts.