sqlalchemy / alembic

A database migrations tool for SQLAlchemy.
MIT License
2.76k stars 240 forks source link

Rendering postgresql dialect DOMAIN field #1360

Open ovangle opened 10 months ago

ovangle commented 10 months ago

Describe the bug

I have the following postgres domain in my model

# adapted from html5 standard
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email#basic_validation
_POSTGRES_EMAIL_RE = (
    r"^"
    r"[a-z0-9.!#$%&''*+/=?^_`{|}~-]+"
    r"@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?"
    r"(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*"
    r"$"
)

EMAIL_DOMAIN = pg_dialect.DOMAIN(
    'email',
    pg_dialect.CITEXT(),
    check=rf"value ~ '{_POSTGRES_EMAIL_RE}'"
) 

class Base(DeclarativeBase):
    pass

class Contact(Base):
    __tablename__ = 'contacts'
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    email: Mapped[str] = mapped_column(EMAIL_DOMAIN)

When autogenerating the field, the column corresponding to the email field is rendered as:

    sa.Column('email', postgresql.DOMAIN('email', CITEXT()), nullable=False),

The rendered output is

Expected behavior The column should be rendered as

    sa.Column('email', postgresql.DOMAIN('email', postgresql.CITEXT(), check=rf"value ~ '^my_.*$'"), nullable=False),

To Reproduce Please try to provide a Minimal, Complete, and Verifiable example, with the migration script and/or the SQLAlchemy tables or models involved. See also Reporting Bugs on the website.

# Insert code here

Error

# Copy error here. Please include the full stack trace.

Versions.

Additional context

Split from issue #1357

Have a nice day!

CaselIT commented 10 months ago

I'm not 100% sure the issue here is in the alembic side or in sqlalchemy (since the type is defined there). At worst we can move the issue to the other repo.

Care to try sending a PR?