Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL. Duplicate table: 7 ERROR: relation" PRIMARY "already exists" 4.0.x #13643

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Expand Up @@ -55,6 +55,7 @@
- Changed `paginate()` in the place of `getPaginate`. Added `previous` in the place of `before`. [#13492](https://github.com/phalcon/cphalcon/issues/13492)
- Scope SQL Column Aliases (on nesting level), in `Phalcon\Mvc\Model\Query`, to prevent overwrite _root_ query's `_sqlColumnAliases` by sub-queries. [#13006](https://github.com/phalcon/cphalcon/issues/13006), [#12548](https://github.com/phalcon/cphalcon/issues/12548) and [#1731](https://github.com/phalcon/cphalcon/issues/1731)
- CLI parameters now work like MVC parameters [#12375](https://github.com/phalcon/cphalcon/pull/12375)
- Changed `Phalcon\Db\Dialect\Postgresql::addPrimaryKey` to make primary key contraints names unique by prefixing them with the table name. [#12629](https://github.com/phalcon/cphalcon/pull/12629)

## Removed
- PHP < 7.2 no longer supported
Expand Down
4 changes: 2 additions & 2 deletions phalcon/db/dialect/postgresql.zep
Expand Up @@ -282,15 +282,15 @@ class Postgresql extends Dialect
*/
public function addPrimaryKey(string! tableName, string! schemaName, <IndexInterface> index) -> string
{
return "ALTER TABLE " . this->prepareTable(tableName, schemaName) . " ADD CONSTRAINT \"PRIMARY\" PRIMARY KEY (" . this->getColumnList(index->getColumns()) . ")";
return "ALTER TABLE " . this->prepareTable(tableName, schemaName) . " ADD CONSTRAINT \"" . tableName . "_PRIMARY\" PRIMARY KEY (" . this->getColumnList(index->getColumns()) . ")";
}

/**
* Generates SQL to delete primary key from a table
*/
public function dropPrimaryKey(string! tableName, string! schemaName) -> string
{
return "ALTER TABLE " . this->prepareTable(tableName, schemaName) . " DROP CONSTRAINT \"PRIMARY\"";
return "ALTER TABLE " . this->prepareTable(tableName, schemaName) . " DROP CONSTRAINT \"" . tableName . "_PRIMARY\"";
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/Db/Dialect/Postgresql/AddIndexCest.php
Expand Up @@ -52,8 +52,8 @@ protected function getAddIndexFixtures()
['schema', 'index1', 'CREATE INDEX "index1" ON "schema"."table" ("column1")'],
['', 'index2', 'CREATE INDEX "index2" ON "table" ("column1", "column2")'],
['schema', 'index2', 'CREATE INDEX "index2" ON "schema"."table" ("column1", "column2")'],
['', 'PRIMARY', 'ALTER TABLE "table" ADD CONSTRAINT "PRIMARY" PRIMARY KEY ("column3")'],
['schema', 'PRIMARY', 'ALTER TABLE "schema"."table" ADD CONSTRAINT "PRIMARY" PRIMARY KEY ("column3")'],
['', 'PRIMARY', 'ALTER TABLE "table" ADD CONSTRAINT "table_PRIMARY" PRIMARY KEY ("column3")'],
['schema', 'PRIMARY', 'ALTER TABLE "schema"."table" ADD CONSTRAINT "table_PRIMARY" PRIMARY KEY ("column3")'],
['', 'index4', 'CREATE UNIQUE INDEX "index4" ON "table" ("column4")'],
['schema', 'index4', 'CREATE UNIQUE INDEX "index4" ON "schema"."table" ("column4")'],
];
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/Db/Dialect/Postgresql/AddPrimaryKeyCest.php
Expand Up @@ -48,8 +48,8 @@ public function dbDialectPostgresqlAddPrimaryKey(IntegrationTester $I)
protected function getAddPrimaryKeyFixtures(): array
{
return [
['', 'PRIMARY', 'ALTER TABLE "table" ADD CONSTRAINT "PRIMARY" PRIMARY KEY ("column3")'],
['schema', 'PRIMARY', 'ALTER TABLE "schema"."table" ADD CONSTRAINT "PRIMARY" PRIMARY KEY ("column3")'],
['', 'PRIMARY', 'ALTER TABLE "table" ADD CONSTRAINT "table_PRIMARY" PRIMARY KEY ("column3")'],
['schema', 'PRIMARY', 'ALTER TABLE "schema"."table" ADD CONSTRAINT "table_PRIMARY" PRIMARY KEY ("column3")'],
];
}
}
12 changes: 6 additions & 6 deletions tests/integration/Db/Dialect/PostgresqlCest.php
Expand Up @@ -242,8 +242,8 @@ protected function getAddIndexFixtures()
['schema', 'index1', 'CREATE INDEX "index1" ON "schema"."table" ("column1")'],
['', 'index2', 'CREATE INDEX "index2" ON "table" ("column1", "column2")'],
['schema', 'index2', 'CREATE INDEX "index2" ON "schema"."table" ("column1", "column2")'],
['', 'PRIMARY', 'ALTER TABLE "table" ADD CONSTRAINT "PRIMARY" PRIMARY KEY ("column3")'],
['schema', 'PRIMARY', 'ALTER TABLE "schema"."table" ADD CONSTRAINT "PRIMARY" PRIMARY KEY ("column3")'],
['', 'PRIMARY', 'ALTER TABLE "table" ADD CONSTRAINT "table_PRIMARY" PRIMARY KEY ("column3")'],
['schema', 'PRIMARY', 'ALTER TABLE "schema"."table" ADD CONSTRAINT "table_PRIMARY" PRIMARY KEY ("column3")'],
['', 'index4', 'CREATE UNIQUE INDEX "index4" ON "table" ("column4")'],
['schema', 'index4', 'CREATE UNIQUE INDEX "index4" ON "schema"."table" ("column4")'],
];
Expand All @@ -255,8 +255,8 @@ protected function getAddIndexFixtures()
protected function getAddPrimaryKeyFixtures(): array
{
return [
['', 'PRIMARY', 'ALTER TABLE "table" ADD CONSTRAINT "PRIMARY" PRIMARY KEY ("column3")'],
['schema', 'PRIMARY', 'ALTER TABLE "schema"."table" ADD CONSTRAINT "PRIMARY" PRIMARY KEY ("column3")'],
['', 'PRIMARY', 'ALTER TABLE "table" ADD CONSTRAINT "table_PRIMARY" PRIMARY KEY ("column3")'],
['schema', 'PRIMARY', 'ALTER TABLE "schema"."table" ADD CONSTRAINT "table_PRIMARY" PRIMARY KEY ("column3")'],
];
}

Expand Down Expand Up @@ -615,8 +615,8 @@ protected function getDropIndexFixtures()
protected function getDropPrimaryKeyFixtures(): array
{
return [
['', 'ALTER TABLE "table" DROP CONSTRAINT "PRIMARY"'],
['schema', 'ALTER TABLE "schema"."table" DROP CONSTRAINT "PRIMARY"'],
['', 'ALTER TABLE "table" DROP CONSTRAINT "table_PRIMARY"'],
['schema', 'ALTER TABLE "schema"."table" DROP CONSTRAINT "table_PRIMARY"'],
];
}

Expand Down