You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// See https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-FK
93
+
Name: "inline column REFERENCES enforces a foreign key constraint",
94
+
SetUpScript: []string{
95
+
`CREATE TABLE parent (a INT PRIMARY KEY)`,
96
+
`CREATE TABLE child (id INT PRIMARY KEY, pid INT REFERENCES parent(a))`,
97
+
`INSERT INTO parent VALUES (1)`,
98
+
},
99
+
Assertions: []ScriptTestAssertion{
100
+
{
101
+
Query: "INSERT INTO child VALUES (1, 1)",
102
+
},
103
+
{
104
+
Query: "INSERT INTO child VALUES (2, 999)",
105
+
ExpectedErr: "Foreign key violation",
106
+
},
107
+
},
108
+
},
109
+
{
110
+
// See https://github.com/morenoh149/postgresDBSamples/blob/master/french-towns-communes-francaises/french-towns-communes-francaises.sql
111
+
Name: "inline column REFERENCES enforces constraints across a chain of related tables",
112
+
SetUpScript: []string{
113
+
`CREATE TABLE regions (id SERIAL UNIQUE NOT NULL, code VARCHAR(4) UNIQUE NOT NULL, name TEXT UNIQUE NOT NULL)`,
114
+
`CREATE TABLE departments (id SERIAL UNIQUE NOT NULL, code VARCHAR(4) UNIQUE NOT NULL, region VARCHAR(4) NOT NULL REFERENCES regions(code), name TEXT UNIQUE NOT NULL)`,
115
+
`CREATE TABLE towns (id SERIAL UNIQUE NOT NULL, code VARCHAR(10) NOT NULL, name TEXT NOT NULL, department VARCHAR(4) NOT NULL REFERENCES departments(code), UNIQUE (code, department))`,
116
+
`INSERT INTO regions VALUES (1, '01', 'Region1')`,
117
+
`INSERT INTO departments VALUES (1, 'D01', '01', 'Department1')`,
118
+
},
119
+
Assertions: []ScriptTestAssertion{
120
+
{
121
+
Query: "INSERT INTO towns VALUES (1, 'T01', 'Town1', 'D01')",
122
+
},
123
+
{
124
+
Query: "INSERT INTO towns VALUES (2, 'T02', 'Town2', 'NOPE')",
0 commit comments