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

ENUM name is not correct for first entry #361

Open
2 tasks done
bvanelli opened this issue Jan 22, 2025 · 2 comments · May be fixed by #363
Open
2 tasks done

ENUM name is not correct for first entry #361

bvanelli opened this issue Jan 22, 2025 · 2 comments · May be fixed by #363
Assignees
Labels
Milestone

Comments

@bvanelli
Copy link

bvanelli commented Jan 22, 2025

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.37

RDBMS vendor

PostgreSQL

What happened?

SQLACodegen causes the first element of the enum to be the enum name, but as far as I know this would be taken literally as an enum value (name goes at the end with the kwarg name, which is also being done). Here are the docs for the PostgresSQL ENUM type.

I'm willing to submit a fix if this is a bug.

Database schema for reproducing the bug

Input:

CREATE SCHEMA definitions;

create type definitions.colors as ENUM
(
    'RED',
    'GREEN',
    'BLUE'
);

CREATE TABLE definitions.state (
    id UUID PRIMARY KEY,
    location TEXT,
    color definitions.colors DEFAULT 'RED'::definitions.colors
);

Output:

from typing import Optional

from sqlalchemy import PrimaryKeyConstraint, Text, Uuid, text
from sqlalchemy.dialects.postgresql import ENUM
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
import uuid

class Base(DeclarativeBase):
    pass


class State(Base):
    __tablename__ = 'state'
    __table_args__ = (
        PrimaryKeyConstraint('id', name='state_pkey'),
        {'schema': 'definitions'}
    )

    id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True)
    location: Mapped[Optional[str]] = mapped_column(Text)
    color: Mapped[Optional[str]] = mapped_column(ENUM('colors', 'RED', 'GREEN', 'BLUE', name='colors'), server_default=text("'RED'::definitions.colors"))
@bvanelli bvanelli added the bug label Jan 22, 2025
@sheinbergon
Copy link
Collaborator

@agronholm this should be an easy fix

@sheinbergon sheinbergon added this to the 3.0 milestone Jan 23, 2025
@sheinbergon sheinbergon self-assigned this Jan 23, 2025
@agronholm
Copy link
Owner

There's a test (test_fancy_coltypes) that tests this but only for plain Tables. There is no equivalent test for declarative classes.

@sheinbergon sheinbergon linked a pull request Jan 24, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants