Foreign Key Setup

45 views
Skip to first unread message

JohanSJA

unread,
Apr 19, 2011, 7:01:07 PM4/19/11
to Lift
I tried to create a very simple application for item management. I
have items in different categories.

// src/main/scala/code/model/Category.scala
package code
package model

import net.liftweb._
import mapper._

class Category extends LongKeyedMapper[Category] with IdPK {
def getSingleton = Category
object name extends MappedPoliteString(this, 50)
}

object Category extends Category with LongKeyedMetaMapper[Category]
with LongCRUDify[Category]

// src/main/scala/code/model/Item.scala
package code
package model

import net.liftweb._
import mapper._

class Item extends LongKeyedMapper[Item] with IdPK {
def getSingleton = Item
object category extends MappedLongForeignKey(this, Category)
object name extends MappedPoliteString(this, 100)
object description extends MappedPoliteString(this, 1000)
}

object Item extends Item with LongKeyedMetaMapper[Item] with
LongCRUDify[Item]

// src/main/scala/bootstrap/liftweb/Boot.scala
...
Schemifier.schemify(true, Schemifier.infoF _, User, Category, Item)
...
Menu.i("Category") / "category" submenus ( Category.menus:_* ),
Menu.i("Item") / "item" submenus ( Item.menus:_* ),
...

The above are the codes that I have added. In addition to that I have
also created two files inside the webapp folder named category.html
and item.html. Things works well for Category. I can doing some simple
CRUD with it. However, the problem is with Item. The category field
cannot be edited, it is just being displayed as can't change instead
of a text field. How can I fix it?

Ján Raska

unread,
Apr 20, 2011, 4:07:34 AM4/20/11
to lif...@googlegroups.com

What do you mean by "cannot be edited"? Do you get some kind of exception? What form object do you display to user to edit category? Do you use select, or radio, or anything else?
Please, be more specific.

Jan

>
> --
> You received this message because you are subscribed to the Google Groups "Lift" group.
> To post to this group, send email to lif...@googlegroups.com.
> To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
>

JohanSJA

unread,
Apr 20, 2011, 4:37:59 AM4/20/11
to Lift
Hi Jan,

By "cannot be edited", I mean I couldn't even have a text field to be
displayed for me to key in the value. I am not in the system that I
have the sample program so I couldn't create a screenshot to show you.

I am expecting a textbox / text field for me to key in the 'category'
value in the 'item' form. However, I am just getting a label / text
showing that 'Can't change'.

I am expecting the user to key in the 'category' value when they try
to create or edit an item. However, textbox / text field did not
appear for them to do so.
>  smime.p7s
> 5KViewDownload

Ján Raska

unread,
Apr 20, 2011, 9:31:42 AM4/20/11
to lif...@googlegroups.com
Ah, I see now, that you're using CRUDify feature. Unfortunatelly, I'm not familiar with that feature, I always create my own snippets to do the job, so I'm sorry, but I can't help you.

JohanSJA

unread,
Apr 20, 2011, 12:00:11 PM4/20/11
to Lift
Hi Jan,

Would you mind to show me the way how you do it without CRUDify?
>  smime.p7s
> 5KViewDownload

Ján Raska

unread,
Apr 20, 2011, 12:59:54 PM4/20/11
to lif...@googlegroups.com
Hi Johan,

I do it by writing snippets (more on snippets can be found here: http://simply.liftweb.net/index-3.4.html#toc-Section-3.4) that either use LiftScreen or SHtml to display a form (http://simply.liftweb.net/index-Chapter-4.html#toc-Chapter-4).

I can make some code example if you can put your current code in a runnable state (maven or sbt) on github or some other place.

Jan

JohanSJA

unread,
Apr 20, 2011, 6:29:50 PM4/20/11
to Lift
Hi Jan,

I have make my code available at https://github.com/JohanSJA/PAW-Stockbook
. Notice that it actually only has a few stuffs in it as I just
started the project. Thanks for your help.
>  smime.p7s
> 5KViewDownload

Ján Raska

unread,
Apr 22, 2011, 2:28:53 PM4/22/11
to lif...@googlegroups.com
Have a look at https://github.com/rusho/paw-example

There are two files to look at - scala/code/snippet/ProductScreen.scala and webapp/productScreen.html

I've written the code very fast as I have very little time currently, so there might be some bugs, but it should at least give you an idea

goo...@ghel-mail.com

unread,
May 8, 2015, 8:21:49 AM5/8/15
to lif...@googlegroups.com, joha...@gmail.com
Hello!

I have exactly the same problem with CRUDify, it's not possible to edit columns that are foreign keys. The content of all other fields can be modified in the browser with textfields, checkboxes, etc., but not the columns which are foreign columns. Instead of a textfield or combobox there is only the text message "Can't change".
I would like to have a textfield with the foreign key integer to modify or much better, a combobox with a specific column of the foreign table instead of the primary key integer.

Does anyone have an idea?

goo...@ghel-mail.com

unread,
May 9, 2015, 7:14:23 AM5/9/15
to lif...@googlegroups.com, joha...@gmail.com
Hello!

I have found a solution. I created my own KeyedMapper, MetaMapper and MappedForeignKey. In the example MyData and MyForeignData are two database tables and MyData contains a foreign key to MyForeignData. CRUDify is included and in the browser you can see and edit the foreign key, displayed with the table column
    override def primaryKeyDisplayField = table_column_field

import scala.xml._
import net.liftweb.common._
import net.liftweb.mapper._
import net.liftweb.util._

trait
MyMapper[OwnerType <: MyMapper[OwnerType]]
   
extends LongKeyedMapper[OwnerType] with IdPK {
 
self: OwnerType =>
}

trait
MyMetaMapper[A <: MyMapper[A]]
   
extends LongKeyedMetaMapper[A]
   
with LongCRUDify[A] {
 
self: A =>
 
def primaryKeyDisplayField: BaseOwnedMappedField[A] = null
}

abstract class MyMappedForeignKey[T<:MyMapper[T], O<:MyMapper[O]](theOwner: T, _foreignMeta: => MyMetaMapper[O])
   
extends MappedLongForeignKey[T, O](theOwner, _foreignMeta) {
 
override def foreignMeta = _foreignMeta
 
override def dbIndexed_? = true
 
override def asHtml = {
   
<span>{foreignMeta.findByKey(this.get)
     
.map(_.fieldByName(foreignMeta.primaryKeyDisplayField.name).openOr(Text("Error"))).openOr(Text("ERROR MyMappedForeignKey"))}</span>
 
}
 
override def validSelectValues/*: Box[List[(Long, String)]]*/ = {
   
Full(foreignMeta.findAll(/*OrderBy(foreignMeta.primaryKeyField, Ascending)*/)
       
.map(i => (i.id.get,
            i
.fieldByName(foreignMeta.primaryKeyDisplayField.name).openOr(Text("ERROR")).toString())))
 
}
}

class MyData extends MyMapper[MyData] {
 
def getSingleton = MyData
 
object myForeignData extends MyMappedForeignKey(this, MyForeignData)
 
object name extends MappedString(this, 30)
}

object MyData extends MyData with MyMetaMapper[MyData] { }

class MyForeignData extends MyMapper[MyForeignData] {
 
def getSingleton = MyForeignData
 
object name extends MappedString(this, 30)
}

object MyForeignData extends MyForeignData with MyMetaMapper[MyForeignData] {
 
override def primaryKeyDisplayField = name
}

Reply all
Reply to author
Forward
0 new messages