MappedLong & null values

25 views
Skip to first unread message

harryh

unread,
Oct 7, 2009, 3:55:59 PM10/7/09
to Lift
I have a long field in my database that I want to have a unique index
(enforced by the database). Sometimes this field will be empty, in
which case I'd like to set it to NULL. 0 won't work because that
won't work with the unique index.

MappedLong appears to be converting null to 0 internally.
Additionally overriding defaultValue for the field and returning null
does not appear to work.

Is this by design? Is it possible that I am misunderstanding something
here?

-harryh

David Pollak

unread,
Oct 7, 2009, 4:36:20 PM10/7/09
to lif...@googlegroups.com
On Wed, Oct 7, 2009 at 12:55 PM, harryh <har...@gmail.com> wrote:

I have a long field in my database that I want to have a unique index
(enforced by the database).  Sometimes this field will be empty, in
which case I'd like to set it to NULL.  0 won't work because that
won't work with the unique index.

MappedLong appears to be converting null to 0 internally.
Additionally overriding defaultValue for the field and returning null
does not appear to work.

Is this by design?

Yes.
 
Is it possible that I am misunderstanding something
here?

I think you're looking for a MappedOptLong which will treat None (or Empty) as NULL and Some(Long) as the value.

Please open a ticket and I'll add the code. 

-harryh





--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

Naftoli Gugenheim

unread,
Oct 11, 2009, 11:22:06 PM10/11/09
to lif...@googlegroups.com
It would be nice if there could be a generalized way of specifying optionality for fields.
(The first though question is whether the default behavior is consistent -- do all field types equate NULL with their default?)
I pasted the names of all the field types (by listing all Mapped*.class files in target/classes/net/liftweb/mapper). For which of them does it make sense to have optionality, which of them do, and would it make sense to have a trait to mix in or some other consistent way to allow nullablility?

MappedBinary, MappedBirthYear, MappedBoolean, MappedCountry, MappedDate, MappedDateTime, MappedDecimal, MappedDouble, MappedEmail, MappedEnum, MappedEnumList, MappedFakeClob, MappedForeignKey, MappedGender, MappedInt, MappedIntIndex, MappedLocale, MappedLong, MappedLongForeignKey, MappedLongIndex, MappedPassword, MappedPoliteString, MappedPostalCode, MappedString, MappedStringForeignKey, MappedStringIndex, MappedText, MappedTextarea, MappedTime, MappedTimeZone, MappedUniqueId

David Pollak

unread,
Oct 12, 2009, 1:31:55 PM10/12/09
to lif...@googlegroups.com
If you've got a good way to do this, feel encouraged to develop it on a branch.

Naftoli Gugenheim

unread,
Oct 12, 2009, 2:04:47 PM10/12/09
to lif...@googlegroups.com
Not offhand, maybe some discussion will generate an good plan.
The easiest and worst way would be to have a marker trait or a flag, and test in each MappedXXX. Obviously not a good idea.
What may work is to delegate all the code that makes decisions which depend on optionality to a method in MappedField, and override that method in a trait that can be mixed in. I haven't actually looked at the relevant Mapper code; just saying a thought.

Reply all
Reply to author
Forward
0 new messages