agronholm / agronholm/sqlacodegen

Support for Native PostgreSQL XML Type

Offen
#483 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
enhancement
Vorherrschende Sprache
Python
Sterne
2.4k
Forks
284
Ø Merge
2 T. 1 Std.
Gemergte PRs (30 T.)
1

Beschreibung

### 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.