1

I wanna change the primary key in one table, but phalcon seems not offer any method to change the primary key.

uid is primary key in this table

+----+------------+------------+------+
| uid    | name      | type        | year |
+----+------------+------------+------+
| 1 | Robotina    | mechanical  | 1972 |
| 2 | Astro Boy   | mechanical | 1952 |
| 3 | Terminator | cyborg      | 2029 |
+----+------------+------------+------+

$tmp = User::findFirst('uid = 100');
$tmp->uid = 900;
$tmp->rate = 15;
$result = $tmp->save();

these codes cannot modify anything in the table , but it return the $result is true, is it a bug? I think it should offer a method to modify primary key.

Linvas
  • 13
  • 2

1 Answers1

2

I don't think it is a bug, I suspect that the Phalcon developers would subscribe to the principle of having primary keys as immutable or set in stone. See questions like : Can we update primary key values of a table? and https://softwareengineering.stackexchange.com/questions/8187/should-a-primary-key-be-immutable/

If anything the bug could be that Phalcon allows you to change the primary key in the first place.

Knowing why you would want to change the primary key might help, because I can't think of any reason off the top of my head why you would want to do that. I believe the main (only?) purpose of the primary key is to identify that row in the table, and any joining tables, whether Robotina has a uid of 1, 11 or 111 shouldn't really matter as long as the entry is unique for all rows in the table.

If the purpose of change is some kind of ranking like a leader board, I would suggest adding a rank field, as it will likely be more efficient that changing a special primary key field.

If you absolutely have to change the uid, I would be inclined to create a new row and delete the old one, if that sounds messy bear in mind that depending on which database you are using that might be what the database engine is doing behind the scenes anyway, and also that the very concept of editing a primary key would set a lot of people's teeth on edge.

Community
  • 1
  • 1
CodeMonkey
  • 3,271
  • 25
  • 50