# Change primary key

**URL:** <https://forums.foundationdb.org/t/change-primary-key/4537>\
**Category:** Record Layer\
**Created:** [June 28, 2024, 12:15pm UTC](https://forums.foundationdb.org/t/change-primary-key/4537 "2024-06-28T12:15:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mping-exo](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/mping-exo/32/1374_2.png) [@mping-exo](https://forums.foundationdb.org/u/mping-exo)\
**Post date:** [June 28, 2024, 12:15pm UTC](https://forums.foundationdb.org/t/change-primary-key/4537/1 "2024-06-28T12:15:56Z")

</div>

Suppose I have the following protobuf

```auto
message Object {
  string id = 1;
  string location = 2;  
  string someattr = 3;
}

```

And recordlayer defines the PK as the record type plus field `id`  
Does the record layer support changing the PK to be record type, `id` and `location`?  
Or the PK is backed by an index that has to be updated, and thus will be in write-only mode?

---

<div class="post-metadata">

**Author:** ![MMcM](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@MMcM](https://forums.foundationdb.org/u/MMcM)\
**Post date:** [July 1, 2024, 7:11pm UTC](https://forums.foundationdb.org/t/change-primary-key/4537/2 "2024-07-01T19:11:46Z")

</div>

There is no way to change a record’s primary key. In some sense, the primary key is the only identity that the record has, so a different primary key is a different record. But, more fundamentally, the primary key determines the physical location of the serialized record. So if changed, the data needs to be rewritten.

In short, the process involves reading the old record with the old record type and writing new records with the new record type.

The indexes would also be a problem, just as you indicate, since they include the old primary key. But they could be updated by scanning the records themselves.

---

<div class="post-metadata">

**Author:** ![mping-exo](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/mping-exo/32/1374_2.png) [@mping-exo](https://forums.foundationdb.org/u/mping-exo)\
**Post date:** [July 1, 2024, 8:29pm UTC](https://forums.foundationdb.org/t/change-primary-key/4537/3 "2024-07-01T20:29:11Z")

</div>

Rewriting the data is OK, but at this point I might as well create a new record type, write the new data, migrate the old data to the new and delete the old data.

Thanks for your input
