Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue when generating Postgres DOMAIN type #329

Open
2 tasks done
yb-leakmited opened this issue Jun 4, 2024 · 4 comments
Open
2 tasks done

Issue when generating Postgres DOMAIN type #329

yb-leakmited opened this issue Jun 4, 2024 · 4 comments
Labels

Comments

@yb-leakmited
Copy link

yb-leakmited commented Jun 4, 2024

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),
)
@agronholm
Copy link
Owner

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

@amacfie
Copy link

amacfie commented Jul 3, 2024

@yb-leakmited try the branch in this PR

@yb-leakmited
Copy link
Author

@agronholm I think it can no ? https://docs.sqlalchemy.org/en/20/dialects/postgresql.html#sqlalchemy.dialects.postgresql.DOMAIN
Well the generator does it, but does not print out the TextClause correctly, thus creating a bug.

@amacfie I'll try your branch to see if it solve my problem.

In any case, I need to go also deep in this repository. I want to generate these Enums, Domains etc... just once and use a reference instead of having duplicated definition.

@amacfie
Copy link

amacfie commented Oct 1, 2024

Yes, ideally the domain is defined once at the top level then referenced by name in columns. Unfortunately my branch includes the domain expression inline in the columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants