agronholm / agronholm/sqlacodegen

Support for Native PostgreSQL XML Type

Abierto
#483 1 comentario 0 reacciones 0 asignados Ver en GitHub
enhancement
Lenguaje dominante
Python
Estrellas
2.4k
Forks
284
Merge medio
2 d 1 h
PR fusionados (30 d)
1

Descripción

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

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.