Commit 9912635c authored by William Naslund's avatar William Naslund

Updated id to use primary key constraint, as it should

parent 903552dd
export * from './constraint';
export * from './foreign-key-constraint';
export * from './unique-constraint';
\ No newline at end of file
export * from './unique-constraint';
export * from './primary-key-constraint';
\ No newline at end of file
import { DBConstraint } from './constraint';
export class DBPrimaryKeyConstraint implements DBConstraint {
constructor(
private fields: string[]
) { }
async create() {
return `PRIMARY KEY (${this.fields.join(', ')})`;
}
getTypeId() { return 'primary_key'; }
}
import { DBAdapter } from "./adapter/database-adapter";
import { DBModelConstructor } from "./model";
import { getTableInformation, getTableFields, getParentLookups } from "./internal/meta-keys";
import { DBUniqueConstraint, DBConstraintConstructor, DBForeignKeyConstraint } from "./constraints";
import { DBUniqueConstraint, DBConstraintConstructor, DBForeignKeyConstraint, DBPrimaryKeyConstraint } from "./constraints";
import { DBTableInformation } from "./decorators";
export class Database {
......@@ -54,14 +54,14 @@ export class Database {
// Id Unique Constraint
if(tableInfo.options && tableInfo.options.id) {
const idConstraint = new DBUniqueConstraint([ String(tableInfo.options.id) ]);
const idConstraint = new DBPrimaryKeyConstraint([ String(tableInfo.options.id) ]);
const idConstraintName = `${tableInfo.name}__id__${idConstraint.getTypeId()}`;
const existingType = await this.getConstraintType(tableInfo, idConstraintName);
if(existingType == null) {
await this.adapter.constraint.create(tableInfo, idConstraintName, idConstraint);
} else {
if(existingType !== DBUniqueConstraint) {
if(existingType !== DBPrimaryKeyConstraint) {
await this.adapter.constraint.drop(tableInfo, idConstraintName);
await this.adapter.constraint.create(tableInfo, idConstraintName, idConstraint);
}
......
import { DBConstraintAdapter, DBConstraintId } from "../adapter/constraint-adapter";
import { DBTableInformation } from "../decorators";
import { DBConstraint, DBUniqueConstraint, DBForeignKeyConstraint } from "../constraints";
import { DBConstraint, DBUniqueConstraint, DBForeignKeyConstraint, DBPrimaryKeyConstraint } from "../constraints";
import { PGAdapter } from "./pg-database-adapter";
/** Constraint adapter for a PostgreSQL database */
......@@ -34,6 +34,9 @@ export class PGConstraintAdapter implements DBConstraintAdapter {
case 'FOREIGN KEY':
id.type = DBForeignKeyConstraint;
break;
case 'PRIMARY KEY':
id.type = DBPrimaryKeyConstraint;
break;
}
if(id.type != null) {
......
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