Jump to content

via Ad Packs

composite primary key not working correctly

composite primary key

  • Please log in to reply
7 replies to this topic

#1 Eran

Eran

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 30 June 2012 - 10: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,
PRIMARY KEY (`projectID`,`appID`)
) 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.

#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 916 posts
  • LocationLondon

Posted 30 June 2012 - 11: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.
Posted Image

#3 Eran

Eran

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 01 July 2012 - 12:05 AM

Hi Johnny,
Thanks for the quick response.
I'll try the work-around you suggested.
I think that using composite primary key is pretty common, so it would be nice to have support for that.
Cheers,
Eran.

#4 Rudolf Byker

Rudolf Byker

    Newbie

  • Members
  • Pip
  • 1 posts

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};
  }


#5 Muhammad Hanif Fahyuananto

Muhammad Hanif Fahyuananto

    Newbie

  • Members
  • Pip
  • 6 posts

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-



#6 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 05 August 2013 - 11:46 AM

Hello Muhammad Hanif Fahyuananto.

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


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#7 mnish

mnish

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationIndia

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. 



#8 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

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. 

 

mnish you could follow the instruction that the core developer gave here: http://www.grocerycr...ctly/#entry2512


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users