Skip to content

Commit

Permalink
Fixed schema prefix in sequence reference messing server defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Apr 17, 2022
1 parent 9d9fafd commit b2a5255
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Version history
- Fixed unwarranted ``ForeignKey`` declarations appearing in column attributes when there are
named, single column foreign key constraints (PR by Leonardus Chen)
. Fixed ``KeyError`` when rendering an index without any columns
- Fixed improper handling of schema prefixes in sequence names in server defaults
- Worked around PostgreSQL UUID columns getting ``Any`` as the type annotation

**3.0.0b3**
Expand Down
16 changes: 13 additions & 3 deletions src/sqlacodegen/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@
_re_enum_check_constraint = re.compile(r"(?:.*?\.)?(.*?) IN \((.+)\)")
_re_enum_item = re.compile(r"'(.*?)(?<!\\)'")
_re_invalid_identifier = re.compile(r"(?u)\W")
_re_postgresql_nextval_sequence = re.compile(r"nextval\('(.+)'::regclass\)")
_re_postgresql_nextval_sequence = re.compile(
r"nextval\('(?:\"(.+?)\"\.)?(.+)'::regclass\)"
)


class CodeGenerator(metaclass=ABCMeta):
Expand Down Expand Up @@ -405,9 +407,17 @@ def render_column(self, column: Column[Any], show_name: bool) -> str:
)
if match:
# Add an explicit sequence
if match.group(1) != f"{column.table.name}_{column.name}_seq":
if match.group(2) != f"{column.table.name}_{column.name}_seq":
callable_kwargs = {}
if match.group(1):
callable_kwargs["schema"] = repr(match.group(1))

args.append(
render_callable("Sequence", repr(match.group(1)))
render_callable(
"Sequence",
repr(match.group(2)),
kwargs=callable_kwargs,
)
)
self.add_literal_import("sqlalchemy", "Sequence")

Expand Down
33 changes: 32 additions & 1 deletion tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest
from _pytest.fixtures import FixtureRequest
from sqlalchemy import PrimaryKeyConstraint
from sqlalchemy import PrimaryKeyConstraint, Sequence
from sqlalchemy.dialects import mysql, postgresql
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.engine import Engine, create_engine
Expand Down Expand Up @@ -933,6 +933,37 @@ def test_postgresql_sequence_nonstandard_name(
""",
)

@pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"])
def test_postgresql_sequence_with_schema(self, generator: CodeGenerator) -> None:
Table(
"simple_items",
generator.metadata,
Column(
"id",
INTEGER,
primary_key=True,
server_default=text("nextval('\"myschema\".test_seq'::regclass)"),
),
schema="myschema",
)

validate_code(
generator.generate(),
"""\
from sqlalchemy import Column, Integer, MetaData, Sequence, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('id', Integer, Sequence('test_seq', schema='myschema'), \
primary_key=True),
schema='myschema'
)
""",
)


class TestDeclarativeGenerator:
@pytest.fixture
Expand Down

0 comments on commit b2a5255

Please sign in to comment.