nocodb icon indicating copy to clipboard operation
nocodb copied to clipboard

πŸ”¦ Feature: Should be able to add unique constraints to columns

Open PrasanthKumar77 opened this issue 2 years ago β€’ 34 comments

Please confirm if feature request does NOT exists already ?

  • [X] I confirm there is no existing issue for this

Describe the usecase for the feature

I have a table where i want to ensure the uniqueness of column. If someone is trying to add a single row/bulk add that was already in database they should get the existing values in database in a readable format to copy. Based on that we will delete the duplicate values in new data and upload the data again.

Suggested Solution

Provide the option to enforce uniqueness in the UI in column dropdown when adding a new column/bulk add. if duplicate values are found then can catch the exception and show it in a readable format in a UI model so that we can identify and remove the duplicate values.

Additional Context

No response

PrasanthKumar77 avatar Dec 30 '22 11:12 PrasanthKumar77

1680420552295 1680420566413 I encountered this issue today.

winterxc avatar Apr 02 '23 07:04 winterxc

image In my NocoDB this option does not appear to prevent duplicate values in the column. Does anyone know how to proceed?

NohanGomes avatar Apr 03 '23 22:04 NohanGomes

There are no NU,PK,AI,UN,AU options in version 105.3. But in my impression, there are these options in previous versions.

sudoexec avatar Apr 06 '23 02:04 sudoexec

@dstala this may be old news to you but it looks to me like at least part of this issue could be resolved by fixing these two lines, which I think would at least allow unique columns to be identified as such. https://github.com/nocodb/nocodb/blob/d9f7aee12101c1d58ee5f0ca7887ba41ecc2a5c0/packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts#LL849C9-L850C95

nicka-redwood avatar May 17 '23 16:05 nicka-redwood

NN - Not NULL PK - Primary Key AI - Auto Increment UN - Unsigned AU - Auto Update (Timestamp)

I understood the requirement. Will discuss with team on this.

dstala avatar Nov 01 '23 13:11 dstala

Versions 0.204.4 and 0.202.6 have been tested and do not take effect. Still reporting an error. 20240312-154645 20240312-154659

mzplus avatar Mar 12 '24 07:03 mzplus

any idea when this could be implemented?

mzramna avatar Mar 22 '24 20:03 mzramna

Can this please be implemented ?

mercuryyy avatar Jul 04 '24 04:07 mercuryyy

Would also benefit greatly from this feature. It's a standard database feature that many people would expect to exist. We will pay $20 USD in Bitcoin to whoever submits a PR to implement this feature.

springfielddatarecovery avatar Aug 13 '24 19:08 springfielddatarecovery

Any update on this? I would love unique column feature. thank you

geekyayush avatar Sep 05 '24 15:09 geekyayush

Would love to see this for the Cloud version using the default Data Source.

chrislentz avatar Sep 09 '24 13:09 chrislentz

NN - Not NULL PK - Primary Key AI - Auto Increment UN - Unsigned AU - Auto Update (Timestamp)

I understood the requirement. Will discuss with team on this.

@dstala I see that this has been put on hold at the moment. Is it planned to be implemented in a future update?

SoCuul avatar Oct 13 '24 10:10 SoCuul

I would love to see UNIQUE column too. We cannot add additional PK in UI., so please allow us to add UNIQUE constrain. That much needed for any kind CRM. p.s. UN is not Unique, but Unsigned. (enforce non negative)

dawidmachon avatar Nov 12 '24 11:11 dawidmachon

Yes please! :-) Just today the use case popped up!

tracure1337 avatar Nov 12 '24 16:11 tracure1337

same here

firstcomeuropeag avatar Nov 24 '24 10:11 firstcomeuropeag

+1, this is pretty much a standard need everywhere where dealing with data

Svashta avatar Dec 11 '24 11:12 Svashta

What I've done is added a constraint in the DB manually. There was a single specific path that worked for me, so here's the full write-up:

  1. Connect to psql: docker exec -i your_container -U your_user -W your_db - you may get '$' or just a cursor, depending on your client
  2. List all relations: \dt *.* - Find your table, make note of the schema
  3. List table detail \d "your_schema.your_table" - IMPORTAINT! All unquoted names are considered lower-case and won't work with variable-case table names
  4. If you got ERROR: relation "your_schema.your_table" does not exist, continue, otherwise skip the next step
  5. Run SET search_path TO your_schema; - Don't forget semicolons! psql returns 'SET' on success; this setting only lasts for the duration of current session
  6. Run ALTER TABLE "your_table" ADD CONSTRAINT your_constraint_name ("Colum1", "Column2"); - Optionally multiple columns, 'ALTER TABLE' returned on success
  7. Congrats! Repeat step 3 to check your new constraint

Notes:

  • SET search_path TO your_schema,public; did not work for me
  • \dt "your_schema.*" would list the table, but \d "your_schema.your_table" would return error
  • \dn+ would show that your_schema has no privileges, but adding those using GRANT ALL ON SCHEMA your_schema TO your_user did not help, even though the user was added to privileges - You may need to include this step too if the above doesn't do it
  • You shouldn't need to select DB, as that's included in step 1, but if you do it's: \c your_db

Hope that helps

filcuk avatar Jan 04 '25 20:01 filcuk

@wingkwong Is there any particular reason this feature was placed indefinitely on hold? What would be the overall difficulty to implement this into the nocodb source.

SoCuul avatar Jan 06 '25 09:01 SoCuul

Another use case just popped up today! +1 for this

xximj avatar Jan 07 '25 16:01 xximj

The same problem in my company's project πŸ‘

arielly-parussulo avatar Feb 12 '25 15:02 arielly-parussulo

Same problem here.

vianamjr avatar Feb 12 '25 15:02 vianamjr

looking forward for that feature!

pkheven avatar Feb 20 '25 16:02 pkheven

Please add the feature!

slashrepeat avatar Feb 22 '25 16:02 slashrepeat

I switch from Coda in order to have a better database management for this internal app I am doing, and then push the data back in Coda. This is my first database in Noco, it looked good but the only feature I really needed was having the ability to "unique", in order to remove dupplicates. I will need to find an altrenative. It's an important feature!

RunLittleTurtle avatar Mar 05 '25 21:03 RunLittleTurtle

Don't know about when they will be adding this feature in UI. But we can achieve the same using API request. So rather defining table through UI, you can create table using API which support unique option for field. API Documentation

I would have loved using combination of field as unique, but at least API gives workaround to field specific uniqueness.

Sample unique field creation API sample. { "title": "uuid", "type": "ID", "pk":1, "pv": 1, "rqd":1, "unique":1 }

amitsquare avatar Mar 21 '25 12:03 amitsquare

+1 for this feature.

Krashnicov avatar Apr 03 '25 09:04 Krashnicov

Image

Joking aside, upon table creation via the UI, a hidden column named id is created and is set as a primary key. This field is an integer and is populated with unique integer values. Here is the formula in our postgres backend: nextval(β€˜β€˜8dhqp380hM083e3.β€œtable_name_id_seq”’::regclass).

Let's say we create a new column named uuid as a pk. The id column will remain a pk, so now our pk columns are id and uuid. Since the id column gives each row a unique identifier, this means we can add duplicate values to our uuid and the rows will still be accepted since the id column makes it unique by default.

One way we have found to fix this is to delete id via our database backend after the creation of uuid and re add it without making it a pk. After this change, if you try to add duplicate uuid values into your table the frontend will correctly tell you that "this row already exists". However this makes the front-end pk feature useless for less technical users.

tobsecret avatar Apr 10 '25 21:04 tobsecret

Hey guys! Bumping this up, just came across this issue today.

mehtaabhi248 avatar Apr 21 '25 10:04 mehtaabhi248

Hey guys! Bumping this up, just came across this issue today.

Upto

Gum97 avatar Apr 22 '25 04:04 Gum97

Come on, devs. It's been 4 years since this issue appeared... WE WANT TO BE NOTICED!

EgorZakharov97 avatar Apr 25 '25 13:04 EgorZakharov97