Commit ec497a5a authored by Simon Riggs's avatar Simon Riggs

Make FKs valid at creation when added as column constraints.

Bug report from Alvaro Herrera
parent 5d1d679d
......@@ -5595,7 +5595,7 @@ ATAddForeignKeyConstraint(AlteredTableInfo *tab, Relation rel,
CONSTRAINT_FOREIGN,
fkconstraint->deferrable,
fkconstraint->initdeferred,
!fkconstraint->skip_validation,
fkconstraint->initially_valid,
RelationGetRelid(rel),
fkattnum,
numfks,
......
......@@ -2341,6 +2341,7 @@ _copyConstraint(Constraint *from)
COPY_SCALAR_FIELD(fk_upd_action);
COPY_SCALAR_FIELD(fk_del_action);
COPY_SCALAR_FIELD(skip_validation);
COPY_SCALAR_FIELD(initially_valid);
return newnode;
}
......
......@@ -2270,6 +2270,7 @@ _equalConstraint(Constraint *a, Constraint *b)
COMPARE_SCALAR_FIELD(fk_upd_action);
COMPARE_SCALAR_FIELD(fk_del_action);
COMPARE_SCALAR_FIELD(skip_validation);
COMPARE_SCALAR_FIELD(initially_valid);
return true;
}
......
......@@ -2625,6 +2625,7 @@ _outConstraint(StringInfo str, Constraint *node)
WRITE_CHAR_FIELD(fk_upd_action);
WRITE_CHAR_FIELD(fk_del_action);
WRITE_BOOL_FIELD(skip_validation);
WRITE_BOOL_FIELD(initially_valid);
break;
case CONSTR_ATTR_DEFERRABLE:
......
......@@ -2621,6 +2621,7 @@ ColConstraintElem:
n->fk_upd_action = (char) ($5 >> 8);
n->fk_del_action = (char) ($5 & 0xFF);
n->skip_validation = FALSE;
n->initially_valid = true;
$$ = (Node *)n;
}
;
......@@ -2820,6 +2821,7 @@ ConstraintElem:
n->deferrable = ($11 & 1) != 0;
n->initdeferred = ($11 & 2) != 0;
n->skip_validation = false;
n->initially_valid = true;
$$ = (Node *)n;
}
| FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
......@@ -2836,6 +2838,7 @@ ConstraintElem:
n->fk_upd_action = (char) ($10 >> 8);
n->fk_del_action = (char) ($10 & 0xFF);
n->skip_validation = true;
n->initially_valid = false;
$$ = (Node *)n;
}
;
......
......@@ -1696,6 +1696,7 @@ transformFKConstraints(CreateStmtContext *cxt,
Constraint *constraint = (Constraint *) lfirst(fkclist);
constraint->skip_validation = true;
constraint->initially_valid = true;
}
}
......
......@@ -1536,6 +1536,7 @@ typedef struct Constraint
char fk_upd_action; /* ON UPDATE action */
char fk_del_action; /* ON DELETE action */
bool skip_validation; /* skip validation of existing rows? */
bool initially_valid; /* start the new constraint as valid */
} Constraint;
/* ----------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment