Re: Multicolumn primary keys + relationships

[prev] [thread] [next] [Date index for 2005/02/24]

From: Peter Speltz
Subject: Re: Multicolumn primary keys + relationships
Date: 08:59 on 24 Feb 2005
--0-669808199-1109235582=:70564
Content-Type: text/plain; charset=us-ascii
Content-Id: 
Content-Disposition: inline

I modified my CDBI::RElationship::MightHave.pm file to support multi column
fks. It was really simple. Just a few lines added and one changed.  Usage is
different in that you specify an extra hashref arg of your Key names and values
after the qw/.../ arg.  The col in the might_have that stores the owner's PK
gets a special key name , '_FK_'.  

So traditional usage was:
Music::CD->might_have(liner_notes => LinerNotes => qw/notes/);

My version you can still say that or you can say:
Music::CD->might_have(front_notes => LinerNotes => qw/notes/,
                    {  _FK_ => 'cd_id', type => 'front' }
);
Music::CD->might_have(back_notes => LinerNotes => qw//, # note blank qw
                     { _FK_ => 'cd_id',  type => 'back' }
);

I haven't tested this a whole lot. just what i needed it for which was mainly
creating the might_have objects automatically from a cd form that had the
inputs for front and back liner notes on it.  One function --
update_create_all_from_cgi -- creates every might_have, has_a, and has_one (my
own rel.) object and relates it to the main object. I'm cleaning that up now.
One caveat is   Multi keyed object may have trouble might having another Multi
keyed object. But it should be easy to fix that too.

If you try it please let me know if you find any bad side effects of my changes
or if you have any ideas.

thanks




--- Crossfire <xfire-cdbi@xxxxx.xx> wrote:

> I'm relatively new to CDBI, and I've become aware that relationships
> using a multicolumn primary key currently are not supported.
> 
> my current db schema has quite a few places where I use a 2-3 column
> primary key, and then use that (with matching foreign key) to refer to
> a parent table...
> 
> From what I've been able to deduct from the documentation, CDBI
> assumes that you only have a single value key if you want to
> use a relationship of any kind.
> 
> Now, I had a look, and attempts to use might_have to link these tables
> failed...
> 
> I had a quick peek, and I would appreciate if anybody can confirm if
> this idea will work or not, if it will break anything major internally
> (nevermind user code just yet - I don't know the gritty details of
> exactly how much carnage a change like this might cause) and if its a
> reasonable idea or not.
> 
> The idea is to to change ->retrieve() to support hashrefs + change
> CDBI's default value to be a suitable reference to a hash of the
> primary key column namess and values if there is more than a single
> column in the primary key.  (Rather than the current
> 'value/value/value' style value for tables with multicolumn primary
> keys)
> 
> With that change, if I'm not mistaken, might_have should be able to
> work with multicolumn primary keys...  (barring any other serious
> issues I may have missed)
> 
> Comments?  Flames?
> 
> C.
> 

=====
pjs

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
--0-669808199-1109235582=:70564
Content-Type: application/x-perl-module; name="MightHave.pm"
Content-Transfer-Encoding: base64
Content-Description: MightHave.pm
Content-Disposition: attachment; filename="MightHave.pm"

cGFja2FnZSBDbGFzczo6REJJOjpSZWxhdGlvbnNoaXA6Ok1pZ2h0SGF2ZTsK
dXNlIHN0cmljdDsKdXNlIHdhcm5pbmdzOwoKdXNlIGJhc2UgJ0NsYXNzOjpE
Qkk6OlJlbGF0aW9uc2hpcCc7CnVzZSBEYXRhOjpEdW1wZXI7IAoKc3ViIHJl
bWFwX2FyZ3VtZW50cyB7CglteSAoJHByb3RvLCAkY2xhc3MsICRtZXRob2Qs
ICRmX2NsYXNzLCBAbWV0aG9kcykgPSBAXzsKCSRjbGFzcy0+X3JlcXVpcmVf
Y2xhc3MoJGZfY2xhc3MpOwoKCSMgcGpzIC0tIHB1dCBjb25zdHJhaW50cyBh
bmQgY29sdW1uIG5hbWUgaW4gYXJncyAKCW15ICgkY29uc3RyYWludHMsICRm
X2NvbCkgPSAoe30sJycpOyAKCSRjb25zdHJhaW50cyA9IHBvcCBAbWV0aG9k
cyAgaWYgKHJlZiAkbWV0aG9kc1stMV0gZXEgIkhBU0giKTsKCSRmX2NvbCA9
IGRlbGV0ZSAkY29uc3RyYWludHMtPntfRktffSB8fCAkZl9jbGFzcy0+cHJp
bWFyeV9jb2x1bW47ICAKCglyZXR1cm4gKCRjbGFzcywgJG1ldGhvZCwgJGZf
Y2xhc3MsIAoJICAgICAgICB7IGZvcmVpZ25fY29sdW1uID0+ICRmX2NvbCwg
CgkJCSAgY29uc3RyYWludHMgPT4g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lY29u
ZGFyeV9hcnRpc3QgPT4gTXVzaWM6OkNEQXJ0aXN0ID0+IAoJCXF3KCksIHtf
RktfPT4nY2RfaWQnLCBhcnRpc3RfdHlwZSA9PiAnc2Vjb25kYXJ5J30gCgkp
OwoKCSMgQ2QgbWlnaHQgaGF2ZSBhbm90aGVyIGFydGlzdAoJCglNdXNpYzo6
Q0QtPm1pZ2h0X2hhdmUoZ3Vlc3Rfc3RhciA9PiBNdXNpYzo6Q0RBcnRpc3Qg
PT4gCgkJcXcoKSwge19GS189PidjZF9pZCcsIGFydGlzdF90eXBlID0+ICdn
dWVzdF9zdGFyJ30gCgkpOwoKCgkjIGdldCBtaWdodF9oYXZlcyBrZXllZCBv
biBmb3JlaWduIGNsYXNzOwoJTXVzaWM6OkJhc2U6Om9wdGlvbmFsX2NvbXBv
bmVudHMgewoJCW15ICgkY2xhc3MsICRmX2NsYXNzKSA9IEBfOwoJCW15ICRv
cHRzID0gICRjbGFzcy0+bWV0YV9pbmZvKCdtaWdodF9oYXZlJyk7CgkJbXkg
JXJlc3VsdDsgCgkJZm9yZWFjaCBteSAkYSAoa2V5cyAlJG9wdHMpIHsKCQkJ
bXkgJGZjbGFzcyA9ICRvcHRzLT57JGF9LT57Zm9yZWlnbl9jbGFzc307CgkJ
CSRyZXN1bHR7JGZjbGFzc30gPSBbXSB1bmxlc3MgJHJlc3VsdHskZmNsYXNz
fTsKCQkJcHVzaCBAeyRyZXN1bHR7JGZjbGFzc319LCAkb3B0cy0+eyRhfTsK
CQl9CgoJCSRmX2NsYXNzID8gcmV0dXJuICRyZXN1bHR7JGZfY2xhc3N9IDog
cmV0dXJuIFwlcmVzdWx0OwoJfQoJCgo9Y3V0Cgo=

--0-669808199-1109235582=:70564--

Multicolumn primary keys + relationships
Crossfire 05:31 on 24 Feb 2005

Re: Multicolumn primary keys + relationships
Peter Speltz 08:59 on 24 Feb 2005

Generated at 22:43 on 24 Feb 2005 by mariachi v0.52