CActiveRecord cannot save() when PK is modified

I use my blog entries’ URLs as primary keys. When they are modified, Yii attempts to update the row identified with new PK which cannot exist at that time.

Update:

I did a workaround so that Post model stores the oldUrl (afterFind()), and I extended update() from CActiveRecord so it calls updateByPk() correctly.

I suggest you NEVER change your primary key.

Primary key should be a value that have no chance to be altered. So, never use names, credit card numbers, urls, paths or something alike. I will avoid SO many problems, not just in Yii.

I’m very aware all of the consequences. This is how it is designed.

This is not about best practices or performance considerations. This is all about bugs and misbehaviors.

Ok, I know that. But Yii works considering you will never change your primary key.

Yes, Yii could maintain the OLD pk. But I’d like know Qiang opinion about this :confused:

My suggestion:

  • Change your primary key to unique key (foreign keys works with unique keys);

  • Create a new primary key, autoincremented.

The consistence will continue the same and you will have the unchanged PK.

See ya!