[DB error] PostgreSQL, for tables without sequences get "Undefined table: ERROR: relation '%_seq' does not exist"
還沒有人認領這個 Issue。
評估
研究方向
從 Codeception\Lib\Driver\PostgreSql::lastInsertId 開始,接著追蹤 Db::haveInDatabase 如何將其用於 PostgreSQL 驅動程式。使用具有整數主鍵但沒有 sequence 的資料表重現此範例,並新增涵蓋測試,顯示在不存在 sequence 時,插入仍能在不出現 undefined-relation 錯誤的情況下完成。
由索引模型根據 Issue 內容生成。
描述
Hi, found this little issue, I get an error when insert a row into a table that has NO sequence, one possible use case - it could happen with UUID as primary key that are generated on an application side, so it's not completely imaginary case :)
I'm using postgres:15, codeception/module-db looks like version 3.1.0,
and for example I have a table (for simplicity, integer primary key, no sequence)
create table t
(
c integer not null primary key
);
Then I call \Codeception\Module\Db::haveInDatabase ($this->tester->haveInDatabase('t', ['c' => 1]);) in my test and see in the console
[DB error] SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "t_c_seq" does not exist
Row is inserted, but after it is trying to extract last inserted ID and (cause it's not possible) it fails.
Happens here in \Codeception\Lib\Driver\PostgreSql::lastInsertId
public function lastInsertId(string $tableName): string
{
$sequenceName = $this->getQuotedName($tableName . '_id_seq');
$lastSequence = null;
try {
$lastSequence = $this->getDbh()->lastInsertId($sequenceName);
} catch (PDOException $exception) {
// in this case, the sequence name might be combined with the primary key name
}
if (!$lastSequence) {
$primaryKeys = $this->getPrimaryKey($tableName);
$pkName = array_shift($primaryKeys);
// next line we get an error
$lastSequence = $this->getDbh()->lastInsertId($this->getQuotedName($tableName . '_' . $pkName . '_seq'));
}
return $lastSequence;
}
And I guess technically that is not an error, because we never should be checking for a last inserted ID in those cases.
Not really sure how to better handle this case, maybe it is possible to add some additional checks before calling \Codeception\Lib\Driver\Db::lastInsertId, trying to detect if a sequence for the given table even exists. If there are no sequence - no need to call \Codeception\Lib\Driver\Db::lastInsertIdmaybe?
In that case I found this monster here https://dba.stackexchange.com/questions/260975/postgresql-how-can-i-list-the-tables-to-which-a-sequence-belongs
SELECT t.oid::regclass AS table_name,
a.attname AS column_name,
s.relname AS sequence_name
FROM pg_class AS t
JOIN pg_attribute AS a
ON a.attrelid = t.oid
JOIN pg_depend AS d
ON d.refobjid = t.oid
AND d.refobjsubid = a.attnum
JOIN pg_class AS s
ON s.oid = d.objid
WHERE d.classid = 'pg_catalog.pg_class'::regclass
AND d.refclassid = 'pg_catalog.pg_class'::regclass
AND d.deptype IN ('i', 'a')
AND t.relkind IN ('r', 'P')
AND s.relkind = 'S';
Maybe it is possible to filter by table and if there is no sequences - return 0 or smth like that. But I'm not sure how to handle different postgresql's version issues - if there's any...
Or add a custom exception and throw it in the lastInsertId method, checking sequence's existence before calling \PDO::lastInsertId. Don't like that one though) But the names for a sequence are building and checking inside this method..
Would be happy to discuss or just hear your thoughts about it, and if I'm lucky even make MR :)
I could try to provide a test that covers that case and fails?
- 主要語言
- PHP
- 星號
- 23
- 分支
- 31
- PR 合併指標
- 30 天內沒有已合併 PR
環境準備
- 提供 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 沒有貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
Codeception/module-db 的其他 Issue
-
難度 3/5 1-2 天 新手友好度 42/100
Codeception/module-db#87 ·
-
難度 4/5 3-5 天 新手友好度 25/100
Codeception/module-db#85 ·
-
Make optional the auto-erase of the records, added by `haveInDatabase()`可能已有人在做 @iliayatsenko 於 675 天前認領。 未關閉
難度 4/5 3-5 天 新手友好度 35/100
Codeception/module-db#68 · 1 則留言 ·
-
Fix a confusion between 'cleanup' and 'skip_cleanup_if_failed' configuration params可能已有人在做 @iliayatsenko 於 675 天前認領。 未關閉
難度 4/5 3-5 天 新手友好度 35/100
Codeception/module-db#67 ·
-
inserting null on an autoincrement primary key prevents cleanup可能已有人在做 @jtopenpetition 於 1213 天前認領。 未關閉
難度 3/5 1-2 天 新手友好度 38/100
Codeception/module-db#58 ·
查看 Codeception/module-db 的全部 Issue
相似的 Issue
-
難度 1/5 1 小時以內 新手友好度 88/100
crazy-goat/rabbit-stream#830 ·
維護者通常 1 天內回覆
-
sync-en
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 1 天內回覆
-
sync-en
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 4 天內回覆
-
Перевод устарел
難度 2/5 1-3 小時 新手友好度 70/100
-
Combination form: image thumbnails collapse to 0×0 when a stylesheet sets `img { max-width: 100% }`未關閉
難度 2/5 1-3 小時 新手友好度 66/100
PrestaShop/PrestaShop#43200 ·
維護者通常 1 天內回覆