[Lift] Re: I am not able to save a many-to-many relationship from an API

2009-10-26 Thread GA
I have made a test that worked. I have modified my code like this:

 newUser.save
 newUser.devices += newDevice
 newUser.save

The newDevice was already saved. It looks like both parents must be  
saved before I save the relationship. Am I right? or I am doing  
something wrong?

Thanks,

GA




On Oct 26, 2009, at 6:55 PM, GA wrote:

 Hello guys,

 I have a many-to-many relationship between two mappers called, Users  
 and Devices.

 There is also an API that receives and XML message that contains one  
 user and one device.

 The API could create the Device and User with the relationship or it  
 could create only the relationship in case the users and/or device  
 already exists.

 The problem I have is that the API creates the User and the Device,  
 but the not the relationship. This is the code that saves the  
 records (just a test for a now):

 def addUser(req: Req): LiftResponse = {

 var tempUserName = 
 var tempDeviceName = 
 var deviceAlreadyExists = false

 val newUser = new User
 val newDevice = new Device
 req.xml match {
 case Full(person{parameters @_*}/person) = {
 for(parameter - parameters){ parameter match {
 case userName{userName}/userName =  
 tempUserName = userName.text
 case firstName{firstName}/firstName  
 = newUser.firstName(firstName.text)
 case lastName{lastName}/lastName =  
 newUser.lastName(lastName.text)
 case password{password}/password =  
 newUser.password(password.text)
 case email{email}/email =  
 newUser.email(email.text)
 case createdon{createdOn}/createdon  
 = newUser.createdOn(new java.util.Date(createdOn.text))
 case updatedon{updatedOn}/updatedon  
 = newUser.updatedOn(new java.util.Date(updatedOn.text))
 case device{deviceName}/device =  
 tempDeviceName = deviceName.text
 case _ =
 }
 }
 try {
 Device.find(By 
 (Device.deviceName,tempDeviceName)) match {
 case Full(deviceRequested) =
 deviceAlreadyExists = true
 case _ = {
 newDevice.deviceName 
 (tempDeviceName)
 newDevice.createdBy(tempUserName)
 newDevice.createdOn 
 (newUser.createdOn)
 newDevice.updatedBy(tempUserName)
 newDevice.updatedOn 
 (newUser.updatedOn)
 newDevice.save
 }
 }
 User.find(By(User.userName, tempUserName))  
 match {
 case Full(userRequested) =
 CreatedResponse(wrapXmlBody 
 (operation id=addPerson success=1/operation), text/xml)
 case _ = {
 newUser.userName(tempUserName)
 newUser.createdBy(tempUserName)
 newUser.updatedBy(tempUserName)
 newUser.devices.clear
 newUser.validated(true)
 newUser.devices += newDevice
 newUser.save
 CreatedResponse(wrapXmlBody 
 (operation id=addPerson success=0/operation), text/xml)
 }
 }
 }
 catch {
 case e = Log.error(Could not add person/ 
 device, e); BadResponse()
 }
 }
 case _ = Log.error(Request was malformed +req.view);  
 BadResponse()
 }
 }

 The field devices is the MappedManyToMany field within the User  
 mapper.

 What am I doing wrong?

 Thanks in advance,

 GA


 


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



[Lift] Re: I am not able to save a many-to-many relationship from an API

2009-10-26 Thread Jim Barrows
On Mon, Oct 26, 2009 at 11:43 AM, GA my_li...@me.com wrote:

 I have made a test that worked. I have modified my code like this:

 newUser.save
 newUser.devices += newDevice
 newUser.save

 The newDevice was already saved. It looks like both parents must be saved
 before I save the relationship. Am I right? or I am doing something wrong?


No, you always have to save the parents before the relationship.  Well.. as
long as the relationship table has the parents PK as it's PK's anyway.
If you remove that restriction, then nah.. it doesn't matter.  Of course
that way lies madness


 Thanks,

 GA




 On Oct 26, 2009, at 6:55 PM, GA wrote:

 Hello guys,

 I have a many-to-many relationship between two mappers called, Users and
 Devices.

 There is also an API that receives and XML message that contains one user
 and one device.

 The API could create the Device and User with the relationship or it could
 create only the relationship in case the users and/or device already exists.

 The problem I have is that the API creates the User and the Device, but the
 not the relationship. This is the code that saves the records (just a test
 for a now):

 def addUser(req: Req): LiftResponse = {

 var tempUserName = 
 var tempDeviceName = 
 var deviceAlreadyExists = false

 val newUser = new User
 val newDevice = new Device
 req.xml match {
 case Full(person{parameters @_*}/person) = {
 for(parameter - parameters){ parameter match {
 case userName{userName}/userName =
 tempUserName = userName.text
 case firstName{firstName}/firstName =
 newUser.firstName(firstName.text)
 case lastName{lastName}/lastName =
 newUser.lastName(lastName.text)
 case password{password}/password =
 newUser.password(password.text)
 case email{email}/email =
 newUser.email(email.text)
 case createdon{createdOn}/createdon =
 newUser.createdOn(new java.util.Date(createdOn.text))
 case updatedon{updatedOn}/updatedon =
 newUser.updatedOn(new java.util.Date(updatedOn.text))
 case device{deviceName}/device =
 tempDeviceName = deviceName.text
 case _ =
 }
 }
 try {
 Device.find(By(Device.deviceName,tempDeviceName))
 match {
 case Full(deviceRequested) =
 deviceAlreadyExists = true
 case _ = {
 newDevice.deviceName(tempDeviceName)
 newDevice.createdBy(tempUserName)
 newDevice.createdOn(newUser.createdOn)
 newDevice.updatedBy(tempUserName)
 newDevice.updatedOn(newUser.updatedOn)
 newDevice.save
 }
 }
 User.find(By(User.userName, tempUserName)) match {
 case Full(userRequested) =
 CreatedResponse(wrapXmlBody(operation
 id=addPerson success=1/operation), text/xml)
 case _ = {
 newUser.userName(tempUserName)
 newUser.createdBy(tempUserName)
 newUser.updatedBy(tempUserName)
 newUser.devices.clear
 newUser.validated(true)
 *newUser.devices += newDevice*
 newUser.save
 CreatedResponse(wrapXmlBody(operation
 id=addPerson success=0/operation), text/xml)
 }
 }
 }
 catch {
 case e = Log.error(Could not add person/device,
 e); BadResponse()
 }
 }
 case _ = Log.error(Request was malformed +req.view);
 BadResponse()
 }
 }

 The field devices is the MappedManyToMany field within the User mapper.

 What am I doing wrong?

 Thanks in advance,

 GA





 



-- 
James A Barrows

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Lift group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 

[Lift] Re: I am not able to save a many-to-many relationship from an API

2009-10-26 Thread GA
Thanks for the answer.

I understand now. But I have a follow up question. This is the code  
for my relationship table, which I've done following some examples and  
previous discussions about this subject:

class UserDevice extends LongKeyedMapper[UserDevice] with IdPK {
 def getSingleton = UserDevice

 object userId extends MappedLongForeignKey(this, User)
 object deviceId extends MappedLongForeignKey(this, Device)
}

object UserDevice extends UserDevice with LongKeyedMetaMapper 
[UserDevice]


The question is:

Shouldn't be both fields (userID and deviceID) be the primary key of  
this table? Why am I using IdPK? Couldn't I just remove it?

Thanks,

GA


On Oct 26, 2009, at 7:49 PM, Jim Barrows wrote:

 On Mon, Oct 26, 2009 at 11:43 AM, GA my_li...@me.com wrote:
 I have made a test that worked. I have modified my code like this:

 newUser.save
 newUser.devices += newDevice
 newUser.save

 The newDevice was already saved. It looks like both parents must be  
 saved before I save the relationship. Am I right? or I am doing  
 something wrong?

 No, you always have to save the parents before the relationship.   
 Well.. as long as the relationship table has the parents PK as it's  
 PK's anyway.
 If you remove that restriction, then nah.. it doesn't matter.  Of  
 course that way lies madness


 Thanks,

 GA




 On Oct 26, 2009, at 6:55 PM, GA wrote:

 Hello guys,

 I have a many-to-many relationship between two mappers called,  
 Users and Devices.

 There is also an API that receives and XML message that contains  
 one user and one device.

 The API could create the Device and User with the relationship or  
 it could create only the relationship in case the users and/or  
 device already exists.

 The problem I have is that the API creates the User and the Device,  
 but the not the relationship. This is the code that saves the  
 records (just a test for a now):

 def addUser(req: Req): LiftResponse = {

 var tempUserName = 
 var tempDeviceName = 
 var deviceAlreadyExists = false

 val newUser = new User
 val newDevice = new Device
 req.xml match {
 case Full(person{parameters @_*}/person) = {
 for(parameter - parameters){ parameter match {
 case userName{userName}/userName =  
 tempUserName = userName.text
 case firstName{firstName}/firstName  
 = newUser.firstName(firstName.text)
 case lastName{lastName}/lastName =  
 newUser.lastName(lastName.text)
 case password{password}/password =  
 newUser.password(password.text)
 case email{email}/email =  
 newUser.email(email.text)
 case createdon{createdOn}/createdon  
 = newUser.createdOn(new java.util.Date(createdOn.text))
 case updatedon{updatedOn}/updatedon  
 = newUser.updatedOn(new java.util.Date(updatedOn.text))
 case device{deviceName}/device =  
 tempDeviceName = deviceName.text
 case _ =
 }
 }
 try {
 Device.find(By 
 (Device.deviceName,tempDeviceName)) match {
 case Full(deviceRequested) =
 deviceAlreadyExists = true
 case _ = {
 newDevice.deviceName 
 (tempDeviceName)
 newDevice.createdBy(tempUserName)
 newDevice.createdOn 
 (newUser.createdOn)
 newDevice.updatedBy(tempUserName)
 newDevice.updatedOn 
 (newUser.updatedOn)
 newDevice.save
 }
 }
 User.find(By(User.userName, tempUserName))  
 match {
 case Full(userRequested) =
 CreatedResponse(wrapXmlBody 
 (operation id=addPerson success=1/operation), text/xml)
 case _ = {
 newUser.userName(tempUserName)
 newUser.createdBy(tempUserName)
 newUser.updatedBy(tempUserName)
 newUser.devices.clear
 newUser.validated(true)
 newUser.devices += newDevice
 newUser.save
 CreatedResponse(wrapXmlBody 
 (operation id=addPerson success=0/operation), text/xml)
 }
 }
  

[Lift] Re: I am not able to save a many-to-many relationship from an API

2009-10-26 Thread Naftoli Gugenheim

It would be feasible to refactor ManyToMany to remove this restriction. 
OneToMany does not require either side to be saved first.
Can you describe a use case where it would be desirable?

-
Jim Barrowsjim.barr...@gmail.com wrote:

On Mon, Oct 26, 2009 at 11:43 AM, GA my_li...@me.com wrote:

 I have made a test that worked. I have modified my code like this:

 newUser.save
 newUser.devices += newDevice
 newUser.save

 The newDevice was already saved. It looks like both parents must be saved
 before I save the relationship. Am I right? or I am doing something wrong?


No, you always have to save the parents before the relationship.  Well.. as
long as the relationship table has the parents PK as it's PK's anyway.
If you remove that restriction, then nah.. it doesn't matter.  Of course
that way lies madness


 Thanks,

 GA




 On Oct 26, 2009, at 6:55 PM, GA wrote:

 Hello guys,

 I have a many-to-many relationship between two mappers called, Users and
 Devices.

 There is also an API that receives and XML message that contains one user
 and one device.

 The API could create the Device and User with the relationship or it could
 create only the relationship in case the users and/or device already exists.

 The problem I have is that the API creates the User and the Device, but the
 not the relationship. This is the code that saves the records (just a test
 for a now):

 def addUser(req: Req): LiftResponse = {

 var tempUserName = 
 var tempDeviceName = 
 var deviceAlreadyExists = false

 val newUser = new User
 val newDevice = new Device
 req.xml match {
 case Full(person{parameters @_*}/person) = {
 for(parameter - parameters){ parameter match {
 case userName{userName}/userName =
 tempUserName = userName.text
 case firstName{firstName}/firstName =
 newUser.firstName(firstName.text)
 case lastName{lastName}/lastName =
 newUser.lastName(lastName.text)
 case password{password}/password =
 newUser.password(password.text)
 case email{email}/email =
 newUser.email(email.text)
 case createdon{createdOn}/createdon =
 newUser.createdOn(new java.util.Date(createdOn.text))
 case updatedon{updatedOn}/updatedon =
 newUser.updatedOn(new java.util.Date(updatedOn.text))
 case device{deviceName}/device =
 tempDeviceName = deviceName.text
 case _ =
 }
 }
 try {
 Device.find(By(Device.deviceName,tempDeviceName))
 match {
 case Full(deviceRequested) =
 deviceAlreadyExists = true
 case _ = {
 newDevice.deviceName(tempDeviceName)
 newDevice.createdBy(tempUserName)
 newDevice.createdOn(newUser.createdOn)
 newDevice.updatedBy(tempUserName)
 newDevice.updatedOn(newUser.updatedOn)
 newDevice.save
 }
 }
 User.find(By(User.userName, tempUserName)) match {
 case Full(userRequested) =
 CreatedResponse(wrapXmlBody(operation
 id=addPerson success=1/operation), text/xml)
 case _ = {
 newUser.userName(tempUserName)
 newUser.createdBy(tempUserName)
 newUser.updatedBy(tempUserName)
 newUser.devices.clear
 newUser.validated(true)
 *newUser.devices += newDevice*
 newUser.save
 CreatedResponse(wrapXmlBody(operation
 id=addPerson success=0/operation), text/xml)
 }
 }
 }
 catch {
 case e = Log.error(Could not add person/device,
 e); BadResponse()
 }
 }
 case _ = Log.error(Request was malformed +req.view);
 BadResponse()
 }
 }

 The field devices is the MappedManyToMany field within the User mapper.

 What am I doing wrong?

 Thanks in advance,

 GA





 



-- 
James A Barrows



--~--~-~--~~~---~--~~
You received this message 

[Lift] Re: I am not able to save a many-to-many relationship from an API

2009-10-26 Thread Jim Barrows
On Mon, Oct 26, 2009 at 12:05 PM, Naftoli Gugenheim naftoli...@gmail.comwrote:


 It would be feasible to refactor ManyToMany to remove this restriction.
 OneToMany does not require either side to be saved first.
 Can you describe a use case where it would be desirable?


I don't think there is.
OneToMany it makes sense that there is no requirement for either side to be
saved first.  However, the pivot table involved in a ManyToMany always needs
both PK's to be unique.


 -
 Jim Barrowsjim.barr...@gmail.com wrote:

 On Mon, Oct 26, 2009 at 11:43 AM, GA my_li...@me.com wrote:

  I have made a test that worked. I have modified my code like this:
 
  newUser.save
  newUser.devices += newDevice
  newUser.save
 
  The newDevice was already saved. It looks like both parents must be saved
  before I save the relationship. Am I right? or I am doing something
 wrong?
 

 No, you always have to save the parents before the relationship.  Well.. as
 long as the relationship table has the parents PK as it's PK's anyway.
 If you remove that restriction, then nah.. it doesn't matter.  Of course
 that way lies madness


  Thanks,
 
  GA
 
 
 
 
  On Oct 26, 2009, at 6:55 PM, GA wrote:
 
  Hello guys,
 
  I have a many-to-many relationship between two mappers called, Users and
  Devices.
 
  There is also an API that receives and XML message that contains one user
  and one device.
 
  The API could create the Device and User with the relationship or it
 could
  create only the relationship in case the users and/or device already
 exists.
 
  The problem I have is that the API creates the User and the Device, but
 the
  not the relationship. This is the code that saves the records (just a
 test
  for a now):
 
  def addUser(req: Req): LiftResponse = {
 
  var tempUserName = 
  var tempDeviceName = 
  var deviceAlreadyExists = false
 
  val newUser = new User
  val newDevice = new Device
  req.xml match {
  case Full(person{parameters @_*}/person) = {
  for(parameter - parameters){ parameter match {
  case userName{userName}/userName =
  tempUserName = userName.text
  case firstName{firstName}/firstName =
  newUser.firstName(firstName.text)
  case lastName{lastName}/lastName =
  newUser.lastName(lastName.text)
  case password{password}/password =
  newUser.password(password.text)
  case email{email}/email =
  newUser.email(email.text)
  case createdon{createdOn}/createdon =
  newUser.createdOn(new java.util.Date(createdOn.text))
  case updatedon{updatedOn}/updatedon =
  newUser.updatedOn(new java.util.Date(updatedOn.text))
  case device{deviceName}/device =
  tempDeviceName = deviceName.text
  case _ =
  }
  }
  try {
  Device.find(By(Device.deviceName,tempDeviceName))
  match {
  case Full(deviceRequested) =
  deviceAlreadyExists = true
  case _ = {
  newDevice.deviceName(tempDeviceName)
  newDevice.createdBy(tempUserName)
 
 newDevice.createdOn(newUser.createdOn)
  newDevice.updatedBy(tempUserName)
 
 newDevice.updatedOn(newUser.updatedOn)
  newDevice.save
  }
  }
  User.find(By(User.userName, tempUserName)) match
 {
  case Full(userRequested) =
  CreatedResponse(wrapXmlBody(operation
  id=addPerson success=1/operation), text/xml)
  case _ = {
  newUser.userName(tempUserName)
  newUser.createdBy(tempUserName)
  newUser.updatedBy(tempUserName)
  newUser.devices.clear
  newUser.validated(true)
  *newUser.devices += newDevice*
  newUser.save
 
 CreatedResponse(wrapXmlBody(operation
  id=addPerson success=0/operation), text/xml)
  }
  }
  }
  catch {
  case e = Log.error(Could not add
 person/device,
  e); BadResponse()
  }
  }
  case _ = Log.error(Request was 

[Lift] Re: I am not able to save a many-to-many relationship from an API

2009-10-26 Thread Naftoli Gugenheim

What do you mean by that? Every primary key is unique.

-
Jim Barrowsjim.barr...@gmail.com wrote:

On Mon, Oct 26, 2009 at 12:05 PM, Naftoli Gugenheim naftoli...@gmail.comwrote:


 It would be feasible to refactor ManyToMany to remove this restriction.
 OneToMany does not require either side to be saved first.
 Can you describe a use case where it would be desirable?


I don't think there is.
OneToMany it makes sense that there is no requirement for either side to be
saved first.  However, the pivot table involved in a ManyToMany always needs
both PK's to be unique.


 -
 Jim Barrowsjim.barr...@gmail.com wrote:

 On Mon, Oct 26, 2009 at 11:43 AM, GA my_li...@me.com wrote:

  I have made a test that worked. I have modified my code like this:
 
  newUser.save
  newUser.devices += newDevice
  newUser.save
 
  The newDevice was already saved. It looks like both parents must be saved
  before I save the relationship. Am I right? or I am doing something
 wrong?
 

 No, you always have to save the parents before the relationship.  Well.. as
 long as the relationship table has the parents PK as it's PK's anyway.
 If you remove that restriction, then nah.. it doesn't matter.  Of course
 that way lies madness


  Thanks,
 
  GA
 
 
 
 
  On Oct 26, 2009, at 6:55 PM, GA wrote:
 
  Hello guys,
 
  I have a many-to-many relationship between two mappers called, Users and
  Devices.
 
  There is also an API that receives and XML message that contains one user
  and one device.
 
  The API could create the Device and User with the relationship or it
 could
  create only the relationship in case the users and/or device already
 exists.
 
  The problem I have is that the API creates the User and the Device, but
 the
  not the relationship. This is the code that saves the records (just a
 test
  for a now):
 
  def addUser(req: Req): LiftResponse = {
 
  var tempUserName = 
  var tempDeviceName = 
  var deviceAlreadyExists = false
 
  val newUser = new User
  val newDevice = new Device
  req.xml match {
  case Full(person{parameters @_*}/person) = {
  for(parameter - parameters){ parameter match {
  case userName{userName}/userName =
  tempUserName = userName.text
  case firstName{firstName}/firstName =
  newUser.firstName(firstName.text)
  case lastName{lastName}/lastName =
  newUser.lastName(lastName.text)
  case password{password}/password =
  newUser.password(password.text)
  case email{email}/email =
  newUser.email(email.text)
  case createdon{createdOn}/createdon =
  newUser.createdOn(new java.util.Date(createdOn.text))
  case updatedon{updatedOn}/updatedon =
  newUser.updatedOn(new java.util.Date(updatedOn.text))
  case device{deviceName}/device =
  tempDeviceName = deviceName.text
  case _ =
  }
  }
  try {
  Device.find(By(Device.deviceName,tempDeviceName))
  match {
  case Full(deviceRequested) =
  deviceAlreadyExists = true
  case _ = {
  newDevice.deviceName(tempDeviceName)
  newDevice.createdBy(tempUserName)
 
 newDevice.createdOn(newUser.createdOn)
  newDevice.updatedBy(tempUserName)
 
 newDevice.updatedOn(newUser.updatedOn)
  newDevice.save
  }
  }
  User.find(By(User.userName, tempUserName)) match
 {
  case Full(userRequested) =
  CreatedResponse(wrapXmlBody(operation
  id=addPerson success=1/operation), text/xml)
  case _ = {
  newUser.userName(tempUserName)
  newUser.createdBy(tempUserName)
  newUser.updatedBy(tempUserName)
  newUser.devices.clear
  newUser.validated(true)
  *newUser.devices += newDevice*
  newUser.save
 
 CreatedResponse(wrapXmlBody(operation
  id=addPerson success=0/operation), text/xml)
  }
  }
  }
  catch {
  case e = Log.error(Could 

[Lift] Re: I am not able to save a many-to-many relationship from an API

2009-10-26 Thread Jim Barrows
On Mon, Oct 26, 2009 at 2:36 PM, Naftoli Gugenheim naftoli...@gmail.comwrote:


 What do you mean by that? Every primary key is unique.


The pivot table is typically only 2 columns, which are the PK's for each
side of the MTM.
----  ---
|Table1|-|Pivot||Table2|
---  ---
PK1PK1PK2
  PK2


Sorry, should have drawn that out earlier :)


 -
 Jim Barrowsjim.barr...@gmail.com wrote:

 On Mon, Oct 26, 2009 at 12:05 PM, Naftoli Gugenheim naftoli...@gmail.com
 wrote:

 
  It would be feasible to refactor ManyToMany to remove this restriction.
  OneToMany does not require either side to be saved first.
  Can you describe a use case where it would be desirable?
 

 I don't think there is.
 OneToMany it makes sense that there is no requirement for either side to be
 saved first.  However, the pivot table involved in a ManyToMany always
 needs
 both PK's to be unique.


  -
  Jim Barrowsjim.barr...@gmail.com wrote:
 
  On Mon, Oct 26, 2009 at 11:43 AM, GA my_li...@me.com wrote:
 
   I have made a test that worked. I have modified my code like this:
  
   newUser.save
   newUser.devices += newDevice
   newUser.save
  
   The newDevice was already saved. It looks like both parents must be
 saved
   before I save the relationship. Am I right? or I am doing something
  wrong?
  
 
  No, you always have to save the parents before the relationship.  Well..
 as
  long as the relationship table has the parents PK as it's PK's anyway.
  If you remove that restriction, then nah.. it doesn't matter.  Of course
  that way lies madness
 
 
   Thanks,
  
   GA
  
  
  
  
   On Oct 26, 2009, at 6:55 PM, GA wrote:
  
   Hello guys,
  
   I have a many-to-many relationship between two mappers called, Users
 and
   Devices.
  
   There is also an API that receives and XML message that contains one
 user
   and one device.
  
   The API could create the Device and User with the relationship or it
  could
   create only the relationship in case the users and/or device already
  exists.
  
   The problem I have is that the API creates the User and the Device, but
  the
   not the relationship. This is the code that saves the records (just a
  test
   for a now):
  
   def addUser(req: Req): LiftResponse = {
  
   var tempUserName = 
   var tempDeviceName = 
   var deviceAlreadyExists = false
  
   val newUser = new User
   val newDevice = new Device
   req.xml match {
   case Full(person{parameters @_*}/person) = {
   for(parameter - parameters){ parameter match {
   case userName{userName}/userName =
   tempUserName = userName.text
   case firstName{firstName}/firstName =
   newUser.firstName(firstName.text)
   case lastName{lastName}/lastName =
   newUser.lastName(lastName.text)
   case password{password}/password =
   newUser.password(password.text)
   case email{email}/email =
   newUser.email(email.text)
   case createdon{createdOn}/createdon =
   newUser.createdOn(new java.util.Date(createdOn.text))
   case updatedon{updatedOn}/updatedon =
   newUser.updatedOn(new java.util.Date(updatedOn.text))
   case device{deviceName}/device =
   tempDeviceName = deviceName.text
   case _ =
   }
   }
   try {
  
 Device.find(By(Device.deviceName,tempDeviceName))
   match {
   case Full(deviceRequested) =
   deviceAlreadyExists = true
   case _ = {
  
 newDevice.deviceName(tempDeviceName)
   newDevice.createdBy(tempUserName)
  
  newDevice.createdOn(newUser.createdOn)
   newDevice.updatedBy(tempUserName)
  
  newDevice.updatedOn(newUser.updatedOn)
   newDevice.save
   }
   }
   User.find(By(User.userName, tempUserName))
 match
  {
   case Full(userRequested) =
   CreatedResponse(wrapXmlBody(operation
   id=addPerson success=1/operation), text/xml)
   case _ = {
   newUser.userName(tempUserName)
   newUser.createdBy(tempUserName)
   newUser.updatedBy(tempUserName)
 

[Lift] Re: I am not able to save a many-to-many relationship from an API

2009-10-26 Thread Naftoli Gugenheim

Do you mean foreign keys? Why does that preclude linking them before they're 
saved? OneToMany also uses a foregn key / primary key relationship, and you can 
use LongMapperMapper.

-
Jim Barrowsjim.barr...@gmail.com wrote:

On Mon, Oct 26, 2009 at 2:36 PM, Naftoli Gugenheim naftoli...@gmail.comwrote:


 What do you mean by that? Every primary key is unique.


The pivot table is typically only 2 columns, which are the PK's for each
side of the MTM.
----  ---
|Table1|-|Pivot||Table2|
---  ---
PK1PK1PK2
  PK2


Sorry, should have drawn that out earlier :)


 -
 Jim Barrowsjim.barr...@gmail.com wrote:

 On Mon, Oct 26, 2009 at 12:05 PM, Naftoli Gugenheim naftoli...@gmail.com
 wrote:

 
  It would be feasible to refactor ManyToMany to remove this restriction.
  OneToMany does not require either side to be saved first.
  Can you describe a use case where it would be desirable?
 

 I don't think there is.
 OneToMany it makes sense that there is no requirement for either side to be
 saved first.  However, the pivot table involved in a ManyToMany always
 needs
 both PK's to be unique.


  -
  Jim Barrowsjim.barr...@gmail.com wrote:
 
  On Mon, Oct 26, 2009 at 11:43 AM, GA my_li...@me.com wrote:
 
   I have made a test that worked. I have modified my code like this:
  
   newUser.save
   newUser.devices += newDevice
   newUser.save
  
   The newDevice was already saved. It looks like both parents must be
 saved
   before I save the relationship. Am I right? or I am doing something
  wrong?
  
 
  No, you always have to save the parents before the relationship.  Well..
 as
  long as the relationship table has the parents PK as it's PK's anyway.
  If you remove that restriction, then nah.. it doesn't matter.  Of course
  that way lies madness
 
 
   Thanks,
  
   GA
  
  
  
  
   On Oct 26, 2009, at 6:55 PM, GA wrote:
  
   Hello guys,
  
   I have a many-to-many relationship between two mappers called, Users
 and
   Devices.
  
   There is also an API that receives and XML message that contains one
 user
   and one device.
  
   The API could create the Device and User with the relationship or it
  could
   create only the relationship in case the users and/or device already
  exists.
  
   The problem I have is that the API creates the User and the Device, but
  the
   not the relationship. This is the code that saves the records (just a
  test
   for a now):
  
   def addUser(req: Req): LiftResponse = {
  
   var tempUserName = 
   var tempDeviceName = 
   var deviceAlreadyExists = false
  
   val newUser = new User
   val newDevice = new Device
   req.xml match {
   case Full(person{parameters @_*}/person) = {
   for(parameter - parameters){ parameter match {
   case userName{userName}/userName =
   tempUserName = userName.text
   case firstName{firstName}/firstName =
   newUser.firstName(firstName.text)
   case lastName{lastName}/lastName =
   newUser.lastName(lastName.text)
   case password{password}/password =
   newUser.password(password.text)
   case email{email}/email =
   newUser.email(email.text)
   case createdon{createdOn}/createdon =
   newUser.createdOn(new java.util.Date(createdOn.text))
   case updatedon{updatedOn}/updatedon =
   newUser.updatedOn(new java.util.Date(updatedOn.text))
   case device{deviceName}/device =
   tempDeviceName = deviceName.text
   case _ =
   }
   }
   try {
  
 Device.find(By(Device.deviceName,tempDeviceName))
   match {
   case Full(deviceRequested) =
   deviceAlreadyExists = true
   case _ = {
  
 newDevice.deviceName(tempDeviceName)
   newDevice.createdBy(tempUserName)
  
  newDevice.createdOn(newUser.createdOn)
   newDevice.updatedBy(tempUserName)
  
  newDevice.updatedOn(newUser.updatedOn)
   newDevice.save
   }
   }
   User.find(By(User.userName, tempUserName))
 match
  {
   case Full(userRequested) =
   CreatedResponse(wrapXmlBody(operation
   id=addPerson success=1/operation),