agronholm / agronholm/sqlacodegen

Support for Native PostgreSQL XML Type

Open
#483 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
2.4k
Forks
284
Avg merge
2d 1h
Merged PRs (30d)
1

Description

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.