Problem in sqlacodegen handling columns with an identity construct.
#265
Unanswered
soypedroamaya
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am doing a project with supabase (postgres) and it has recommended me to make my table like this:
create table "public"."sexual_genders" (
"id" smallint generated by default as identity not null,
"gender" text not null
);
CREATE UNIQUE INDEX sexual_genders_gender_key ON public.sexual_genders USING btree (gender);
CREATE UNIQUE INDEX sexual_genders_pkey ON public.sexual_genders USING btree(id);
alter table "public"."sexual_genders" add constraint "sexual_genders_pkey" PRIMARY KEY using index "sexual_genders_pkey";
alter table "public"."sexual_genders" add constraint "sexual_genders_gender_key" UNIQUE using index "sexual_genders_gender_key";
But when executing the ./sqlcodegen.sh script to build the sqlalchemy files, it gave me the following error:
main.py:47: SAWarning: Skipped unsupported reflection of expression-based index users_instance_id_email_idx
metadata.reflect(engine, args.schema, not args.noviews, tables)
Traceback (most recent call last):
File "/bend-calcetinesperdidos/venv-exp/bin/sqlacodegen", line 8, in
sys.exit(main())
File "/bend-calcetinesperdidos/venv-exp/lib/python3.10/site-packages/sqlacodegen/main.py", line 53, in main
generator.render(outfile)
File "/bend-calcetinesperdidos/venv-exp/lib/python3.10/site-packages/sqlacodegen/codegen.py", line 727, in render
rendered_models.append(self.render_class(model))
File "/bend-calcetinesperdidos/venv-exp/lib/python3.10/site-packages/sqlacodegen/codegen.py", line 707, in render_class
self.indentation, attr, self.render_column(column, show_name))
File "/bend-calcetinesperdidos/venv-exp/lib/python3.10/site-packages/sqlacodegen/codegen.py", line 601, in render_column
default_expr = self._get_compiled_expression(column.server_default.arg)
AttributeError: 'Identity' object has no attribute 'arg'
And helping me with ChatGPT tells me the following:
The error you are experiencing seems to be related to a problem in
sqlacodegenhandling columns with an identity construct. The column with the identity construct in question appears to have anIdentityobject, butsqlacodegenexpects an object with anargattribute.This problem may be caused by a specific version of
sqlacodegenorSQLAlchemythat does not fully support identity construction.To fix this problem, you can try the following:
Update
sqlacodegenandSQLAlchemyto the latest available versions:If the problem persists even after upgrading, you might consider applying a temporary patch to
sqlacodegen. First, locate thecodegen.pyfile within your virtual environment. In your case, it seems to be in the following path:Open the
codegen.pyfile in a text editor and look for therender_columnfunction. Inside this function, add a condition to check if theserver_defaultobject is an instance ofIdentity. If so, skip generating theargattribute.For example, you can modify the corresponding function as follows:
def render_column(self, column, show_name):
kwarg = []
is_sole_pk = column.primary_key and len(column.table.primary_key) == 1
dedicated_fks = [c for c in column.foreign_keys if len(c.constraint.columns) == 1]
is_unique = any(isinstance(c, UniqueConstraint) and set(c.columns) == {column}
for c in column.table.constraints)
is_unique = is_unique or any(i.unique and set(i.columns) == {column}
for i in column.table.indexes)
has_index = any(set(i.columns) == {column} for i in column.table.indexes)
server_default = None
Please note that this fix is only a temporary patch and may not be fully compatible with future versions of
sqlacodegenorSQLAlchemy. I suggest you follow the releases of these libraries and upgrade to a version that natively resolves this issue when available.Results:
And it worked, but it seems appropriate to mention that sqlacodegen is not supporting these postgres features that supabase recommends when using their dashboard studio to build tables.
Beta Was this translation helpful? Give feedback.
All reactions