add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
-- Add FK:
CREATE TABLE serie (
id_serie int PRIMARY KEY
);
INSERT INTO serie VALUES (6), (7), (8), (9) RETURNING *;

-- enum
CREATE TYPE serie_aluno AS ENUM ('6', '7', '8', '9');

CREATE TABLE aluno (
id int
, serie_aluno serie_aluno
);

INSERT INTO aluno VALUES
(1, '6')
, (2, '7')
, (3, null) -- works too
RETURNING *;
CREATE TABLE
id_serie
6
7
8
9
INSERT 0 4
CREATE TYPE
CREATE TABLE
id serie_aluno
1 6
2 7
3 null
INSERT 0 3
-- enum cast demo
SELECT 6 AS t_int
, '6' AS t_text
, '6'::serie_aluno AS t_enum
, '6'::serie_aluno::text AS cast_text
, '6'::serie_aluno::text::int AS cast_int_via_proxy -- !!!
;
t_int t_text t_enum cast_text cast_int_via_proxy
6 6 6 6 6
SELECT 1
-- ERROR: cannot cast type serie_aluno to integer
SELECT '6'::serie_aluno::int AS cast_enum;
ERROR:  cannot cast type serie_aluno to integer
LINE 2: SELECT  '6'::serie_aluno::int AS cast_enum;
                                ^
-- proper copmmand to change type *and* add FK
ALTER TABLE aluno
ALTER COLUMN serie_aluno TYPE integer USING serie_aluno::text::int
, ADD CONSTRAINT serie_aluno_serie_fkey FOREIGN KEY (serie_aluno) REFERENCES serie(id_serie)
;

TABLE aluno;
ALTER TABLE
id serie_aluno
1 6
2 7
3 null
SELECT 3
-- FK violation
INSERT INTO aluno VALUES (4, 10);
ERROR:  insert or update on table "aluno" violates foreign key constraint "serie_aluno_serie_fkey"
DETAIL:  Key (serie_aluno)=(10) is not present in table "serie".
-- Valid FK
INSERT INTO aluno VALUES (4, 9);

TABLE aluno;
INSERT 0 1
id serie_aluno
1 6
2 7
3 null
4 9
SELECT 4