sqlacodegen
sqlacodegen copied to clipboard
Issue when generating Postgres DOMAIN type
Things to check first
-
[X] I have searched the existing issues and didn't find my bug already reported there
-
[X] 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 :
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 : https://github.com/sqlalchemy/sqlalchemy/commit/017fd9ae0645eaf2a0fbdd067d10c721505b018c
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
@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.
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.