agronholm / agronholm/sqlacodegen

Support for Native PostgreSQL XML Type

Aperta
#483 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
enhancement
Lingua principale
Python
Stelle
2.4k
Fork
284
Merge medio
2g 1h
PR unite (30g)
1

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.