Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

654
Views
Postgres: establezca un índice único en el campo JSON del tipo de matriz

Hay una tabla como la siguiente:

 CREATE TABLE test (my_field jsonb NOT NULL)

my_field tiene una clave como collection que almacena valores en formato varchar[] :

 {my_field: {collection: ['a', 'b', 'c']}}

Sé que es posible establecer un índice único para valores json simples. Por ejemplo, para {my_field: {name: 'Someone'}} , se podría establecer una restricción única. (ya lo probé)

He intentado exclude using gist mucho, pero hasta ahora no tengo nada.

¿Es posible establecer una restricción única (considerando la superposición) en esta situación?

Gracias por adelantado.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Si no se trata de ordenar, el índice simple funcionará:

 so=# CREATE TABLE test (my_field jsonb NOT NULL); CREATE TABLE so=# insert into test select '{"my_field": {"collection": ["a", "b", "c"]}}'; INSERT 0 1 so=# create unique index uk_c on test ((my_field->'my_field'->>'collection')); CREATE INDEX so=# insert into test select '{"my_field": {"collection": ["a", "b", "c"]}}'; ERROR: duplicate key value violates unique constraint "uk_c" DETAIL: Key (((my_field -> 'my_field'::text) ->> 'collection'::text))=(["a", "b", "c"]) already exists.
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!