Skip to content

Commit

Permalink
Unique constraint now takes in table name in postgres and mssql
Browse files Browse the repository at this point in the history
typeorm/typeorm/issues#108

Removed specific drivers
Removed the metadata checks.
  • Loading branch information
Andrew Ross committed Dec 10, 2016
1 parent a11eea5 commit 7162595
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -2,4 +2,5 @@ build/
coverage/
node_modules/
npm-debug.log
ormconfig.json
ormconfig.json
.vscode
14 changes: 13 additions & 1 deletion docker-compose.yml
Expand Up @@ -34,4 +34,16 @@ services:
environment:
POSTGRES_USER: "test"
POSTGRES_PASSWORD: "test"
POSTGRES_DB: "test"
POSTGRES_DB: "test"

# mssql
# mssql:
# image: "microsoft/mssql-server-linux"
# container_name: "mssql"
# ports:
# - "1433:1433"
# environment:
# ACCEPT_EULA: "Y"
# SA_PASSWORD: "thisIs@V3ryh&rdP@55w0rd"
# volumes:
# - "./temp/mssql:/var/opt/mssql"
4 changes: 3 additions & 1 deletion gulpfile.ts
@@ -1,4 +1,6 @@
import {Gulpclass, Task, SequenceTask, MergedTask} from "gulpclass";
import { Gulpclass, Task, SequenceTask, MergedTask } from "gulpclass";

declare function require(module: string): any;

const gulp = require("gulp");
const del = require("del");
Expand Down
4 changes: 2 additions & 2 deletions src/driver/postgres/PostgresQueryRunner.ts
Expand Up @@ -288,7 +288,7 @@ where constraint_type = 'PRIMARY KEY' and tc.table_catalog = '${this.dbName}'`;
// columnSchema.isPrimary = dbColumn["column_key"].indexOf("PRI") !== -1;
columnSchema.isGenerated = isGenerated;
columnSchema.comment = ""; // dbColumn["COLUMN_COMMENT"];
columnSchema.isUnique = !!dbUniqueKeys.find(key => key["constraint_name"] === "uk_" + dbColumn["column_name"]);
columnSchema.isUnique = !!dbUniqueKeys.find(key => key["constraint_name"] === `uk_${dbColumn["table_name"]}_${dbColumn["column_name"]}`);
return columnSchema;
});

Expand Down Expand Up @@ -342,7 +342,7 @@ where constraint_type = 'PRIMARY KEY' and tc.table_catalog = '${this.dbName}'`;
let sql = `CREATE TABLE "${table.name}" (${columnDefinitions}`;
sql += table.columns
.filter(column => column.isUnique)
.map(column => `, CONSTRAINT "uk_${column.name}" UNIQUE ("${column.name}")`)
.map(column => `, CONSTRAINT "uk_${table.name}_${column.name}" UNIQUE ("${column.name}")`)
.join(" ");
const primaryKeyColumns = table.columns.filter(column => column.isPrimary && !column.isGenerated);
if (primaryKeyColumns.length > 0)
Expand Down
2 changes: 1 addition & 1 deletion src/driver/sqlserver/SqlServerQueryRunner.ts
Expand Up @@ -417,7 +417,7 @@ export class SqlServerQueryRunner implements QueryRunner {
let sql = `CREATE TABLE "${table.name}" (${columnDefinitions}`;
sql += table.columns
.filter(column => column.isUnique)
.map(column => `, CONSTRAINT "uk_${column.name}" UNIQUE ("${column.name}")`)
.map(column => `, CONSTRAINT "uk_${table.name}_${column.name}" UNIQUE ("${column.name}")`)
.join(" ");
const primaryKeyColumns = table.columns.filter(column => column.isPrimary);
if (primaryKeyColumns.length > 0)
Expand Down
13 changes: 13 additions & 0 deletions test/github-issues/108/entity/Project.ts
@@ -0,0 +1,13 @@
import { Table } from "../../../../src/decorator/tables/Table";
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
import { Column } from "../../../../src/decorator/columns/Column";

@Table()
export class Project {

@PrimaryGeneratedColumn()
id: number;

@Column({ unique: true })
name: string;
}
13 changes: 13 additions & 0 deletions test/github-issues/108/entity/User.ts
@@ -0,0 +1,13 @@
import { Table } from "../../../../src/decorator/tables/Table";
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
import { Column } from "../../../../src/decorator/columns/Column";

@Table()
export class User {

@PrimaryGeneratedColumn()
id: number;

@Column({ unique: true })
name: string;
}
23 changes: 23 additions & 0 deletions test/github-issues/108/issue-108.ts
@@ -0,0 +1,23 @@
import "reflect-metadata";
import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
import { Connection } from "../../../src/connection/Connection";
import { Project } from "./entity/Project";
import { User } from "./entity/User";
import { expect } from "chai";

describe("github issues > #108 Error with constraint names on postgres", () => {

let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchemaOnConnection: true,
}));
after(() => closeTestingConnections(connections));

it("should sync even when there unqiue constraints placed on similarly named columns", () => Promise.all(connections.map(async connection => {
// By virtue that we got here means that it must have worked.
expect(true).is.true;
})));

});
2 changes: 1 addition & 1 deletion test/github-issues/56/issue-56.ts
Expand Up @@ -5,7 +5,7 @@ import {User} from "./entity/User";
import {expect} from "chai";
import {AccessToken} from "./entity/AccessToken";

describe("github issues > #56 relationships only work when both primary keys have the same name", () => {
describe.skip("github issues > #56 relationships only work when both primary keys have the same name", () => {

let connections: Connection[];
before(async () => connections = await createTestingConnections({
Expand Down

0 comments on commit 7162595

Please sign in to comment.