agronholm / agronholm/sqlacodegen

Support for Native PostgreSQL XML Type

オープン
#483 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る
enhancement
主要言語
Python
スター
2.4k
フォーク
284
平均マージ
2日 1時間
マージ済み PR(30日)
1

説明

### Things to check first

- [x] I have searched the existing issues and didn't find my feature already requested there

### Feature description

Hi there,

Postgres's native `xml` type isn't recognized during reflection. It currently falls back to `NullType`, which produces a `Mapped[Any]` annotation. Instead, it should generate something like `raw_metadata: Mapped[str] = mapped_column(XML, nullable=False)`.

## Current Behvior

```sql
CREATE TABLE documents (
id SERIAL PRIMARY KEY,
raw_metadata XML NOT NULL
);
```

`sqlacodegen postgresql+psycopg://user:pw@localhost/mydb`:

warning:
> Did not recognize type 'xml' of column 'raw_metadata'

Generated code:
```python
from typing import Any

from sqlalchemy import Integer, PrimaryKeyConstraint
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy.sql.sqltypes import NullType

class Base(DeclarativeBase):
pass

class Documents(Base):
__tablename__ = 'documents'
__table_args__ = (
PrimaryKeyConstraint('id', name='documents_pkey'),
)

id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
raw_metadata: Mapped[Any] = mapped_column(NullType, nullable=False)
```

## Expected Behavior

Instead, I'd like to have an XML type with a `str` annotation: `raw_metadata: Mapped[str] = mapped_column(XML, nullable=False)`

For my current custom type registration workaround, see https://github.com/EOSC-Data-Commons/metadata-warehouse/issues/154#issuecomment-5117131145

### Use case

We are using Postgres's native `xml` type in our DB schema since we rely on functionalities like `XPATH` expressions. We think since `xml` is supported by Postgres and is supposedly widely used, it might be useful to have support for this in the generated Python models.

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

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

評価

この issue はまだ評価されていません。

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

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