Skip to content

Commit

Permalink
Ability to name references constraint (#290)
Browse files Browse the repository at this point in the history
* Ability to name references constraint

* Extending test
  • Loading branch information
dolezel authored Jun 29, 2018
1 parent a5006f5 commit 2084852
Showing 5 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/columns.md
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ The `createTable` and `addColumns` methods both take a `columns` argument that s
- `default` _[string]_ - adds DEFAULT clause for column. Accepts null, a literal value, or a `pgm.func()` expression.
- `check` _[string]_ - sql for a check constraint for this column
- `references` _[string]_ - a table name that this column is a foreign key to
- `referencesConstraintName` _[string]_ - name of the created constraint
- `onDelete` _[string]_ - adds ON DELETE constraint for a reference column
- `onUpdate` _[string]_ - adds ON UPDATE constraint for a reference column
- `match` _[string]_ - `FULL` or `SIMPLE`
1 change: 1 addition & 0 deletions docs/constraints.md
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
- `foreignKeys` _[object or array of objects]_ - foreign keys specification
- `columns` _[string or array of strings]_ - names of columns
- `references` _[string]_ - names of foreign table and column names
- `referencesConstraintName` _[string]_ - name of the created constraint
- `onDelete` _[string]_ - action to perform on delete
- `onUpdate` _[string]_ - action to perform on update
- `match` _[string]_ - `FULL` or `SIMPLE`
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ export type Type = string | { type: string }

export interface ForeignKeyOptions {
columns: Name | Name[]
referencesConstraintName?: string
references?: Name
onDelete?: string
onUpdate?: string
16 changes: 13 additions & 3 deletions lib/operations/tables.js
Original file line number Diff line number Diff line change
@@ -11,12 +11,22 @@ const {
} = require("../utils");

const parseReferences = options => {
const { references, match, onDelete, onUpdate } = options;
const clauses = [
const {
references,
referencesConstraintName,
match,
onDelete,
onUpdate
} = options;
const clauses = [];
if (referencesConstraintName) {
clauses.push(`CONSTRAINT "${referencesConstraintName}"`);
}
clauses.push(
typeof references === "string"
? `REFERENCES ${references}`
: template`REFERENCES "${references}"`
];
);
if (match) {
clauses.push(`MATCH ${match}`);
}
12 changes: 12 additions & 0 deletions test/migrations/071_constraint_name_for_foreign_key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports.up = pgm => {
pgm.createTable("ft1", { id: "id" });
pgm.createTable("ft2", {
id: {
type: "integer",
notNull: true,
references: "ft1",
referencesConstraintName: "my_constraint_name"
}
});
pgm.renameConstraint("ft2", "my_constraint_name", "better_constraint_name");
};

0 comments on commit 2084852

Please sign in to comment.