Commit 0d123dd7 authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

PSQL: make foreign key DEFERRABLE INITIALLY DEFERRED

parent 39aff54e
......@@ -1336,6 +1336,14 @@ bool DataStore::beginTransaction()
TRANSACTION_MUTEX_UNLOCK;
return false;
}
if (DbType::type(m_database) == DbType::PostgreSQL) {
// Make constraints check deferred in PostgreSQL. Allows for
// INSERT INTO mimetypetable (name) VALUES ('foo') RETURNING id;
// INSERT INTO collectionmimetyperelation (collection_id, mimetype_id) VALUES (x, y)
// where "y" refers to the newly inserted mimetype
m_database.exec(QStringLiteral("SET CONSTRAINTS ALL DEFERRED"));
}
}
++m_transactionLevel;
......
......@@ -300,7 +300,8 @@ QString DbInitializerPostgreSql::buildAddForeignKeyConstraintStatement(const Tab
const QString constraintName = table.name + column.name + QLatin1Literal("_") + column.refTable + column.refColumn + QLatin1Literal("_fk");
return QLatin1Literal("ALTER TABLE ") + table.name + QLatin1Literal(" ADD CONSTRAINT ") + constraintName + QLatin1Literal(" FOREIGN KEY (") + column.name
+ QLatin1Literal(") REFERENCES ") + column.refTable + QLatin1Literal("Table(") + column.refColumn
+ QLatin1Literal(") ") + buildReferentialAction(column.onUpdate, column.onDelete);
+ QLatin1Literal(") ") + buildReferentialAction(column.onUpdate, column.onDelete)
+ QLatin1Literal(" DEFERRABLE INITIALLY DEFERRED");
}
QString DbInitializerPostgreSql::buildRemoveForeignKeyConstraintStatement(const DbIntrospector::ForeignKey &fk, const TableDescription &table) const
......
Supports Markdown
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