RE: Quick $obj->delete qn where obj has multiple primary keys
[prev]
[thread]
[next]
[Date index for 2005/07/01]
This is a multi-part message in MIME format.
------_=_NextPart_001_01C57E0A.4FF1F4B9
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
> As far as I know delete() works just fine for multi-column=20
> primary keys.
> If it didn't I expect a lot of people would have been=20
> screaming by now.
I've attached a demonstration script that will create a sqlite file
called 'test.sqlite' in the current working directory. Run it with no
arguments for a successful run, with any arguments for the fail case.
The setup in question is a link table and as far as I can tell the same
as the many-to-many Actor, Film and Role example in the docs.
The only difference is that I have changed the string overloading on the
Actor and Film classes to be a nicer "name" field like so:
__PACKAGE__->columns (Stringify =3D> qw/name/);
This addition causes things to break occasionally. Things behave
differently depending on whether the relationship already exists in
Roles or whether it is being created along the way. The following code
doesn't work if find_or_create() found a pre-existing entry:
$r =3D Role->find_or_create( actor =3D> $actor, film =3D> $film );
# if the above created the entry in the DB then the below will work
# if the above retrieved an existing entry from the DB then it won't
delete it here
$r->delete;
The has_a() documentation mentions that the objects are deflated by
stringification unless the deflate method is specified so I thought
adding an explicit deflate() for all relationships that involve modified
stringifications might fix up the weirdness but no.
Would this be an example of the bad things described in the note at the
bottom of the has_a() section: "Note: You should not attempt to make
your primary key column inflate using has_a as bad things will happen"?
How have other people directly manipulated a link table like this?
I'm guessing at this point that I either:
1. Change things in my application to call my own stringification
method rather than relying on a modified override and proceed
as normal.
Or
2. Have my own manipulation methods in all my link classes to do
operations like delete.
Any advice is welcome.
Cheers,
- ao
------_=_NextPart_001_01C57E0A.4FF1F4B9
Content-Type: application/octet-stream;
name="cdbi_delete.pl"
Content-Transfer-Encoding: base64
Content-Description: cdbi_delete.pl
Content-Disposition: attachment;
filename="cdbi_delete.pl"
IyEvdXNyL2Jpbi9wZXJsIC13CgpwYWNrYWdlIE15OjpEQkk7CnVzZSBiYXNlIHF3KENsYXNzOjpE
QkkpOwpfX1BBQ0tBR0VfXy0+YXV0b3VwZGF0ZSgxKT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aWV2ZSggMSApOyAgIyBuYW1lID0+ICdUZXN0IFZlbmRvciAxJwpteSAk
Y29udGFjdCA9IE15OjpDb250YWN0LT5yZXRyaWV2ZSggMSApOyAjIG5hbWUgPT4gJ1Rlc3QgQ29u
dGFjdCAxJwoKbXkgJGxpbmsgPSBNeTo6TGlua1ZlbmRvckNvbnRhY3QtPmZpbmRfb3JfY3JlYXRl
KCB2ZW5kb3IgPT4gJHZlbmRvciwgY29udGFjdCA9PiAkY29udGFjdCk7Cndhcm4gRHVtcGVyKCdP
cmlnaW5hbCBvYmplY3Q6JywkbGluayk7CgojTXk6OkxpbmtWZW5kb3JDb250YWN0LT5kYl9NYWlu
LT50cmFjZSgxKTsKJGxpbmstPmRlbGV0ZTsKI015OjpMaW5rVmVuZG9yQ29udGFjdC0+ZGJfTWFp
bi0+dHJhY2UoMCk7Cndhcm4gRHVtcGVyKCdEZWxldGVkIGxpbms6JywkbGluayk7CgpNeTo6TGlu
a1ZlbmRvckNvbnRhY3QtPmNsZWFyX29iamVjdF9pbmRleCBpZiAkZmFpbDsKCiRsaW5rID0gTXk6
OkxpbmtWZW5kb3JDb250YWN0LT5yZXRyaWV2ZSh2ZW5kb3IgPT4gJHZlbmRvciwgY29udGFjdCA9
PiAkY29udGFjdCk7Cndhcm4gRHVtcGVyKCdUaGlzIHNob3VsZCBiZSB1bmRlZjonLCAkbGluayk7
Cg==
------_=_NextPart_001_01C57E0A.4FF1F4B9--
|
|
RE: Quick $obj->delete qn where obj has multiple primary keys
Andrew O'Brien 06:58 on 01 Jul 2005
|