Add primary key with Data Tables?

What I’m trying to do:
Make a primary key on a column in a table using Data Tables.

What I’ve tried and what’s not working:
I’ve tried looking at the documentation, and right clicking the column in the table to see if there was an option to set as primary key, but didn’t see anything.

There’s probably some KEY understanding I don’t have here :wink:

Hi there,

Haha. Great pun.

I don’t think you can “mark” a specific column as a primary key in Anvil’s native data tables.

If your goal is to define a uniqueness constraint, that’s not built in. To enforce any kind of constraint, it’s often best to wrap your table’s logical “insert” (“delete”, “update”, …) operation in a function, and have that function enforce all the constraints.

For best performance and security, this is usually done in a Server Module.

This old post might be relevant:

I use the built-in row id (or links between tables) for situations in which (I think) you’d traditionally use a primary key, but I’m very much an amateur with such things.

1 Like

I’m afraid you’re right… I might try and move this to a feature request. Not having the ability to create primary keys natively is a huge problem.

@hugetim , that is useful information for sure! Thank you.

I’m looking for the more traditional way to just be able to use the GUI to right click a column and set it as a PK.

1 Like