Type.ReplaceTableKeys
TypeReturns a table type with all key definitions replaced by the provided list of key records.
Syntax
Type.ReplaceTableKeys(type as type, keys as list) as typeParameters
| Name | Type | Required | Description |
|---|---|---|---|
type | type | Yes | The base table type whose keys are replaced. |
keys | list | Yes | A list of key records, each with Columns (list of text) and Primary (logical) fields. Pass an empty list to remove all keys. |
Return Value
type — A new table type with the specified key definitions.
Remarks
Type.ReplaceTableKeys replaces all existing key definitions on a table type with a completely new set of keys provided as a list. Each element of the list must be a record with two fields:
- Columns — a list of column name text values forming the key
- Primary — true for the primary key, false for alternate keys
Passing an empty list ({}) removes all keys from the type.
Type.ReplaceTableKeys is the bulk-replacement counterpart to Type.AddTableKey. Use it when you want to set the complete key configuration in a single call — for example, when rebuilding a schema from scratch or applying a key set derived from external metadata. Use Type.AddTableKey when you only need to add one key at a time on top of existing keys.
Like all type functions in M, Type.ReplaceTableKeys returns a new type value — it does not modify the original. To apply the resulting type to an actual table, use Value.ReplaceType(table, newType). Use Type.TableKeys to verify the result.
Examples
Example 1: Replace existing keys with a new primary key
let
T = #table(type table [ID = number, Name = text], {}),
OldType = Value.Type(T),
NewType = Type.ReplaceTableKeys(OldType, {[Columns = {"ID"}, Primary = true]}),
Keys = Type.TableKeys(NewType)
in
Keys{0}[Primary]The final output — reads the Primary field of the first key to confirm it is true.
Result | |
|---|---|
| 1 | TRUE |
Example 2: Clear all keys by passing an empty list
let
T = #table(type table [ID = number, Name = text], {}),
TypeWithKey = Type.AddTableKey(Value.Type(T), {"ID"}, true),
TypeNoKeys = Type.ReplaceTableKeys(TypeWithKey, {}),
Keys = Type.TableKeys(TypeNoKeys)
in
List.Count(Keys)The final output — retrieves the key list from the updated type using Type.TableKeys, then counts it to confirm zero keys remain.
Result | |
|---|---|
| 1 | 0 |
Example 3: Replace with both a primary key and an alternate key
let
T = #table(type table [OrderID = number, Email = text, Product = text], {}),
NewType = Type.ReplaceTableKeys(
Value.Type(T),
{
[Columns = {"OrderID"}, Primary = true],
[Columns = {"Email"}, Primary = false]
}
),
Keys = Type.TableKeys(NewType)
in
List.Count(Keys)The final output — retrieves the key list from the new type using Type.TableKeys, then counts it to confirm two keys are present.
Result | |
|---|---|
| 1 | 2 |