agronholm/sqlacodegen

Issue when generating Postgres DOMAIN type

yb-leakmited opened this issue · 4 comments

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Sqlacodegen version

3.0.0rc5

SQLAlchemy version

2.0.30

RDBMS vendor

PostgreSQL

What happened?

I used sqlacodegen --generator tables to generate my SQLAlchemy tables. But it seems that for Domain it is not working properly.
I have a Domain type that is the following :

image

sqlacodegen generates the following line:

....
Column('lang', DOMAIN('language_tag_type', TEXT(), constraint_name='valid_language_tag', not_null=False, check= < sqlalchemy.sql.elements.TextClause object at 0x000002173F60A1D0 >), nullable=False),
....

The check string is not correctly generated. If think It just needs to call to_string() instead of giving the TextClause object no ?

Related closed PR on SQLAlchemy : sqlalchemy/sqlalchemy@017fd9a

Note : I started python & SQLAchemy not long ago
Note 2 : I'll try to create a repo to reproduce this bug
Note 3 : THANK YOU FOR CREATING THIS TOOL 😄 , it is simple and amazing, hopefully one day it will be SQLAlchemy and maintained by it

Database schema for reproducing the bug

CREATE DOMAIN public.language_tag_type
    AS text;

ALTER DOMAIN public.language_tag_type
    ADD CONSTRAINT valid_language_tag CHECK (VALUE ~ '^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$'::text);

CREATE TABLE IF NOT EXISTS public."user"
(
    id uuid NOT NULL DEFAULT 'gen_random_uuid()',
    lang language_tag_type COLLATE pg_catalog."default" NOT NULL,
    CONSTRAINT user_pkey PRIMARY KEY (id),
)

I don't think DOMAIN types can be reflected by SQLAlchemy, at this point anyway.

@yb-leakmited try the branch in this PR