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

382
Views
Verifique la restricción en la clave más grande de HSTORE en Postgres

Me gustaría crear una restricción de verificación en el campo HSTORE que contiene datos en el siguiente formato:

 { 1 => 2020-03-01, 2 => 2020-03-07, etc, etc, etc, }

Donde clave es siempre un dígito positivo y valor es una fecha.

Problema aquí que quiero extraer claves (por akeys ), y luego de alguna manera obtener la clave más grande y compararla con number_of_episodes (entero positivo). Pero dice que no puedo usar matrices en la restricción de verificación.

La pregunta es: ¿es posible extraer de alguna manera la clave más grande de HSTORE como un número entero y luego usarla en la restricción de verificación?

Gracias.

 alter table archives_seasonmodel add constraint test check (max((unnest(akeys(episodes))) <= number_of_episodes ))

esto no funciona

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Esto funciona para mí en PostgreSQL 10:

 # create table tvseries (number_of_episodes int, episodes hstore, check (number_of_episodes >= all (akeys(episodes)::int[])) ); CREATE TABLE # insert into tvseries values (2, '1=>"a", 2=>"b"'); INSERT 0 1 # insert into tvseries values (1, '1=>"a", 2=>"b"'); ERROR: new row for relation "tvseries" violates check constraint "tvseries_check" DETAIL: Failing row contains (1, "1"=>"a", "2"=>"b"). # insert into tvseries values (2, '1=>"a"'); INSERT 0 1 # select * from tvseries; number_of_episodes | episodes --------------------+-------------------- 2 | "1"=>"a", "2"=>"b" 2 | "1"=>"a" (2 rows)
over 4 years ago · Santiago Trujillo Report

0

Esta respuesta describe un par de formas en que puede hacer esto. La primera es usar la extensión intarray y su función sort_desc , pero creo que el mejor enfoque aquí es usar una función personalizada.

 testdb=# create extension hstore; CREATE EXTENSION testdb=# create table tt0(h hstore, max_n bigint); CREATE TABLE testdb=# CREATE OR REPLACE FUNCTION array_greatest(anyarray) RETURNS anyelement LANGUAGE SQL AS $$ SELECT max(x) FROM unnest($1) as x; $$; CREATE FUNCTION testdb=# alter table tt0 add check((array_greatest(akeys(h)::integer[]))<=max_n); ALTER TABLE testdb=# insert into tt0 select hstore(ARRAY[['1','asdf'],['3','fdsa']]), 2; ERROR: new row for relation "tt0" violates check constraint "tt0_check" DETAIL: Failing row contains ("1"=>"asdf", "3"=>"fdsa", 2). testdb=# insert into tt0 select hstore(ARRAY[['1','asdf'],['2','fdsa']]), 2; INSERT 0 1 testdb=# select * from tt0 testdb-# ; h | max_n --------------------------+------- "1"=>"asdf", "2"=>"fdsa" | 2 (1 row) testdb=# \d tt0 Table "public.tt0" Column | Type | Collation | Nullable | Default --------+--------+-----------+----------+--------- h | hstore | | | max_n | bigint | | | Check constraints: "tt0_check" CHECK (array_greatest(akeys(h)::integer[]) <= max_n)
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!