AnswerDotAI / AnswerDotAI/sqlite-minutils

`transform=True` fails when you have an index on a column name with an underscore

オープン
#24 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Python
スター
17
フォーク
9
PR マージ指標
30日以内にマージされた PR はありません

説明

I'm using this project (sqlite_minutils) as a part of a FastHTML web app and came across a bug. I used `python=3.12` and `sqlite-minutils==3.37.0.post3`. Here's a minimal reproduction:

```python
#script.py

from fasthtml.common import Database
from datetime import datetime

db = Database('test.db')

class Request:
id: str
time: str
# run with time_check and its index commented out first
time_check: str

requests = db.create(Request, if_not_exists=True, transform=True)
requests.create_index(('time',), unique=True, if_not_exists=True)
# first run with this index creation commented out
requests.create_index(('time_check',), unique=True, if_not_exists=True)

for i in range(5):
requests.insert(Request(time=datetime.now()))
```
To reproduce, simply first comment out `time_check: str` and the index on `time_check`, run `python script.py`. Then uncomment `time_check` and its index creation and re-run `python script.py`. You will get the following error, which is caused by the query returning nothing, and the index `[0]` trying to grab what isn't there.

```bash
File venv/lib/python3.12/site-packages/sqlite_minutils/db.py:1910, in Table.transform_sql(self, types, rename, drop, pk, not_null, defaults, drop_foreign_keys, add_foreign_keys, foreign_keys, column_order, tmp_suffix, keep_table)
1908 for index in self.indexes:
1909 if index.origin not in ("pk"):
-> 1910 index_sql = self.db.execute(
1911 """SELECT sql FROM sqlite_master WHERE type = 'index' AND name = :index_name;""",
1912 {"index_name": index.name},
1913 ).fetchall()[0][0]
1914 assert index_sql is not None, (
1915 f"Index '{index}' on table '{self.name}' does not have a "
1916 "CREATE INDEX statement. You must manually drop this index prior to running this "
1917 "transformation and manually recreate the new index after running this transformation."
1918 )
1919 if keep_table:

IndexError: list index out of range
```

It looks like the parameter substitution fails when there is an extra underscore from the column name. If I do the same query manually using the `?` substitution syntax it works, and if I do the query manually typing out the index name verbatim it also works.

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

調査の方向性

sqlite_minutils/db.py の Table.transform_sql から始め、特に 1910 行付近の sqlite_master の検索を確認し、script.py で issue の再現を実行します。アンダースコアを含む名前に対するパラメーター化されたインデックス名のクエリを確認してください。変換が IndexError なしで完了し、対象のインデックスが保持されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python, sqlite
領域
databases
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。