Problem with primary key made up of several fields

View: New views
4 Messages — Rating Filter:   Alert me  

Problem with primary key made up of several fields

by zpon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hello

I have a sql table which only contains two field (user_id and
group_id), these fields also makes up a primary key, but whenever I
try to delete from the table (del(array('user_id' => $this->data...,
'group_id' => $this->data...)) I get the following error:

SELECT COUNT(*) AS `count` FROM `users_groups` AS `UsersGroups` WHERE
`UsersGroups`.`id` = 'users_groups.user_id'

I have tried to set the primaryKey in the model to the two correct
fields, but that didn't help.

What am I doing wrong? Thanks in advance

ps. I have also set belongsTo:
var $belongsTo = array(
        'Groups' => array(
                'className' => 'Groups',
                'foreignKey' => 'group_id'),
        'User' => array(
                'className' => 'User',
                'foreignKey' => 'user_id'));

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


Re: Problem with primary key made up of several fields

by James K :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


CakePHP does not support composite primary keys. You need to add
another column to your table called id and make that the primary key.
To ensure this doesn't break your database, you can add a unique
constraint to user_id and group_id at the database level. Kinda sucks,
but it's the only option unless you want to hand-write your own update
and delete queries.

On Jul 7, 3:38 am, zpon <zpon...@...> wrote:

> Hello
>
> I have a sql table which only contains two field (user_id and
> group_id), these fields also makes up a primary key, but whenever I
> try to delete from the table (del(array('user_id' => $this->data...,
> 'group_id' => $this->data...)) I get the following error:
>
> SELECT COUNT(*) AS `count` FROM `users_groups` AS `UsersGroups` WHERE
> `UsersGroups`.`id` = 'users_groups.user_id'
>
> I have tried to set the primaryKey in the model to the two correct
> fields, but that didn't help.
>
> What am I doing wrong? Thanks in advance
>
> ps. I have also set belongsTo:
> var $belongsTo = array(
>         'Groups' => array(
>                 'className' => 'Groups',
>                 'foreignKey' => 'group_id'),
>         'User' => array(
>                 'className' => 'User',
>                 'foreignKey' => 'user_id'));
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@...
To unsubscribe from this group, send email to cake-php-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Problem with primary key made up of several fields

by Jonathan Snook-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


You'd want to get rid of the $belongsTo relationship. This is just a
HABTM relationship (the composite primary key here shouldn't be a
problem, although I don't believe Cake will enforce it.).

On Mon, Jul 7, 2008 at 6:38 AM, zpon <zpon.dk@...> wrote:

> I have a sql table which only contains two field (user_id and
> group_id), these fields also makes up a primary key, but whenever I
> try to delete from the table (del(array('user_id' => $this->data...,
> 'group_id' => $this->data...)) I get the following error:
>
> SELECT COUNT(*) AS `count` FROM `users_groups` AS `UsersGroups` WHERE
> `UsersGroups`.`id` = 'users_groups.user_id'
>
> I have tried to set the primaryKey in the model to the two correct
> fields, but that didn't help.
>
> What am I doing wrong? Thanks in advance
>
> ps. I have also set belongsTo:

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


Re: Problem with primary key made up of several fields

by zpon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Okay, i went with you solution, I guess it will be about the same,
although the composite primary key was kind of nice :-)
Thanks to everybody answering

On 7 Jul., 14:29, James K <james.m.k...@...> wrote:

> CakePHP does not support composite primary keys. You need to add
> another column to your table called id and make that the primary key.
> To ensure this doesn't break your database, you can add a unique
> constraint to user_id and group_id at the database level. Kinda sucks,
> but it's the only option unless you want to hand-write your own update
> and delete queries.
>
> On Jul 7, 3:38 am, zpon <zpon...@...> wrote:
>
> > Hello
>
> > I have a sql table which only contains two field (user_id and
> > group_id), these fields also makes up a primary key, but whenever I
> > try to delete from the table (del(array('user_id' => $this->data...,
> > 'group_id' => $this->data...)) I get the following error:
>
> > SELECT COUNT(*) AS `count` FROM `users_groups` AS `UsersGroups` WHERE
> > `UsersGroups`.`id` = 'users_groups.user_id'
>
> > I have tried to set the primaryKey in the model to the two correct
> > fields, but that didn't help.
>
> > What am I doing wrong? Thanks in advance
>
> > ps. I have also set belongsTo:
> > var $belongsTo = array(
> >         'Groups' => array(
> >                 'className' => 'Groups',
> >                 'foreignKey' => 'group_id'),
> >         'User' => array(
> >                 'className' => 'User',
> >                 'foreignKey' => 'user_id'));

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

LightInTheBox - Buy quality products at wholesale price!