On Mon, Oct 26, 2009 at 3:42 PM, Naftoli Gugenheim <[email protected]>wrote:

>
> 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.
>


Yes, the pivot table has 2 foreign keys as it's primary keys.  The pivots
tables primay key is typically a compound key of Table1 & Table2's PK's.
You can't save the pivot table before Table1 & 2 because the primary keys
for Table 1&2 don't exist yet in the database.  You can update in any order
(provided the PK's aren't changing of course).



> -------------------------------------
> Jim Barrows<[email protected]> wrote:
>
> On Mon, Oct 26, 2009 at 2:36 PM, Naftoli Gugenheim <[email protected]
> >wrote:
>
> >
> > 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|
> -----------            --------          -----------
> PK1                PK1            PK2
>                      PK2
>
>
> Sorry, should have drawn that out earlier :)
>
>
> > -------------------------------------
> > Jim Barrows<[email protected]> wrote:
> >
> > On Mon, Oct 26, 2009 at 12:05 PM, Naftoli Gugenheim <
> [email protected]
> > >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 Barrows<[email protected]> wrote:
> > >
> > > On Mon, Oct 26, 2009 at 11:43 AM, GA <[email protected]> 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
> > >
> > >
> > >
> > > >
> > >
> >
> >
> > --
> > James A Barrows
> >
> >
> >
> > >
> >
>
>
> --
> James A Barrows
>
>
>
> >
>


-- 
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to