⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

composite primary key not working correctly



Eran
  • profile picture
  • Member

Posted 30 June 2012 - 22:34 PM

Hi,
I'm building an app that contains several tables.
I noticed that when I try to use "Edit" or "Delete" on a table that has a composite primary key (a primary key that is made up of 2 or more columns), the operations tend to fail.
It seems that this occurs because grocery curd just uses the first column in the primary key.
Here is an example table and data:

--
-- Table structure for table `t_profiles`
--

DROP TABLE IF EXISTS `t_profiles`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `t_profiles` (
`projectID` int(11) NOT NULL,
`appID` varchar(100) NOT NULL,
`appVer` varchar(45) NOT NULL,
`mgrFileName` varchar(100) NOT NULL,
`mgrArgs` varchar(250) NOT NULL,
[b] PRIMARY KEY (`projectID`,`appID`)[/b]
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `t_profiles`
--

LOCK TABLES `t_profiles` WRITE;
/*!40000 ALTER TABLE `t_profiles` DISABLE KEYS */;
INSERT INTO `t_profiles` VALUES (1,'dummy app 2','1.0.0.2','dummyMan2.0.0.4.zip',''),(1,'dummy app1','4.0.0.7','dummyMan2.0.0.4.zip','');
/*!40000 ALTER TABLE `t_profiles` ENABLE KEYS */;
UNLOCK TABLES;


Try to edit one of the 2 rows using grocery curd and you will notice that you can't.
Notice that the links to edit and delete use only the value of "projectID".

I tried using set_primary_key but I couldn't figure out how to add the 2 columns. So I tried adding both of them one after another and that also didn't work.
Am I missing something?
Is there a quick fix that I can use to get it to work?

Thanks,
Eran.

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 30 June 2012 - 23:45 PM

Hello Eran.

This is actually a feature that don't exist yet in grocery CRUD. It is a method that I want to add in the future with name "set_secondary_key" . So to do that you have to change the core functionality of grocery CRUD.

For now the only work-around that I can suggest is to have another field auto-increment primary key with name for example profile_id and have the two `projectID`,`appID` as KEY and not as a primary key. I know that probably you don't want to change the structure of your database but at least it is a fast solution.

Eran
  • profile picture
  • Member

Posted 01 July 2012 - 00:05 AM

Hi Johnny,
Thanks for the quick response.
I'll try the work-around you suggested.
I think that using [color=#282828][font=helvetica, arial, sans-serif]composite primary key is pretty common, so it would be nice to have support for that.[/font][/color]
Cheers,
Eran.

Rudolf Byker
  • profile picture
  • Member

Posted 03 January 2013 - 11:47 AM

+1 to this!! A very important feature IMHO. B)

As far as I can see, the changes need to be made in grocery_crud.php near line 1439:


foreach($data->list as $num_row => $row)
{
$data->list[$num_row]->edit_url = $data->edit_url.'/'.$row->{$data->primary_key};
$data->list[$num_row]->delete_url = $data->delete_url.'/'.$row->{$data->primary_key};
}

Muhammad Hanif Fahyuananto
  • profile picture
  • Member

Posted 03 August 2013 - 03:43 AM

Have the new Grocery CRUD apply the composite key mechanism?
I have the same problem now, must I change the composite key become individual key and make a new primary key which created from auto increment ?
whether the set_secondary_key have been developed? or which part that I can change to solve this ?

-Thanks before-


davidoster
  • profile picture
  • Member

Posted 05 August 2013 - 11:46 AM

Hello [member=muhammad hanif fahyuananto].

I am afraid the composite key feature on the latest stable version 1.4 is not present.


mnish
  • profile picture
  • Member

Posted 30 August 2013 - 06:38 AM

Double Primary Key not considered or Composite Primary Key Not Work

How to use Grocery Crud When Two Primary Keys Are Present In Table  None Of them is Auto Increment. 


davidoster
  • profile picture
  • Member

Posted 31 August 2013 - 07:36 AM

Double Primary Key not considered or Composite Primary Key Not Work

How to use Grocery Crud When Two Primary Keys Are Present In Table  None Of them is Auto Increment. 

 

[member=mnish] you could follow the instruction that the core developer gave here: /topic/639-composite-primary-key-not-working-correctly/#entry2512


Carlos Bernal
  • profile picture
  • Member

Posted 12 August 2014 - 21:16 PM

This is a very important feature indeed. Any one knows where the changes should be made?


Andre
  • profile picture
  • Member

Posted 16 March 2016 - 21:49 PM

Hello guys my first post here! I found this very good library because i'm working on a grocery store! eheheh Searched for a grocery crud and found exactly what i was looking for! Thank you.

Now, i am also working a lot with 2 foreign keys as a composite key for a lot of tables in my project. The obvious work around would be a primary key with auto_increment, which is perfectly fine, although a feature for this "problem" can be a very useful addition to the project!

Thank you and and very good work!