Skip to content

Commit bbdf5ff

Browse files
dkratzertpre-commit-ci[bot]agronholmsheinbergon
authored
Double pluralization of names (#262)
* prevent relationship names to be double pluralized * Add test for double pluralized relationship * Add test for singular table name * Makes sure that a singular table name with a one-to-many relationship does not end up with a relationship name of 'False' because of self.inflect_engine.singular_noun(preferred_name) returns False with a singular preferred_name in generate_relationship_name(). * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Shorten long line * Delete test_generators.py * Re-added double_pluralization tests * Updated change log * Update src/sqlacodegen/generators.py Co-authored-by: Alex Grönholm <[email protected]> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Alex Grönholm <[email protected]> Co-authored-by: Idan Sheinberg <[email protected]>
1 parent 1626080 commit bbdf5ff

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Version history
99
(PR by @LajosCseppento)
1010
- Fixed incorrect package name used in ``importlib.metadata.version`` for
1111
``sqlalchemy-citext``, resolving ``PackageNotFoundError`` (PR by @oaimtiaz)
12+
- Prevent double pluralization (PR by @dkratzert)
1213

1314
**3.0.0**
1415

src/sqlacodegen/generators.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,9 +1076,8 @@ def generate_relationship_name(
10761076
RelationshipType.ONE_TO_MANY,
10771077
RelationshipType.MANY_TO_MANY,
10781078
):
1079-
inflected_name = self.inflect_engine.plural_noun(preferred_name)
1080-
if inflected_name:
1081-
preferred_name = inflected_name
1079+
if not self.inflect_engine.singular_noun(preferred_name):
1080+
preferred_name = self.inflect_engine.plural_noun(preferred_name)
10821081
else:
10831082
inflected_name = self.inflect_engine.singular_noun(preferred_name)
10841083
if inflected_name:

tests/test_generator_declarative.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,66 @@ class SimpleItem(Base):
10031003
)
10041004

10051005

1006+
@pytest.mark.parametrize("generator", [["use_inflect"]], indirect=True)
1007+
def test_use_inflect_plural_double_pluralize(generator: CodeGenerator) -> None:
1008+
Table(
1009+
"users",
1010+
generator.metadata,
1011+
Column("users_id", INTEGER),
1012+
Column("groups_id", INTEGER),
1013+
ForeignKeyConstraint(
1014+
["groups_id"], ["groups.groups_id"], name="fk_users_groups_id"
1015+
),
1016+
PrimaryKeyConstraint("users_id", name="users_pkey"),
1017+
)
1018+
1019+
Table(
1020+
"groups",
1021+
generator.metadata,
1022+
Column("groups_id", INTEGER),
1023+
Column("group_name", Text(50), nullable=False),
1024+
PrimaryKeyConstraint("groups_id", name="groups_pkey"),
1025+
)
1026+
1027+
validate_code(
1028+
generator.generate(),
1029+
"""\
1030+
from typing import Optional
1031+
1032+
from sqlalchemy import ForeignKeyConstraint, Integer, PrimaryKeyConstraint, Text
1033+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
1034+
1035+
class Base(DeclarativeBase):
1036+
pass
1037+
1038+
1039+
class Group(Base):
1040+
__tablename__ = 'groups'
1041+
__table_args__ = (
1042+
PrimaryKeyConstraint('groups_id', name='groups_pkey'),
1043+
)
1044+
1045+
groups_id: Mapped[int] = mapped_column(Integer, primary_key=True)
1046+
group_name: Mapped[str] = mapped_column(Text(50))
1047+
1048+
users: Mapped[list['User']] = relationship('User', back_populates='group')
1049+
1050+
1051+
class User(Base):
1052+
__tablename__ = 'users'
1053+
__table_args__ = (
1054+
ForeignKeyConstraint(['groups_id'], ['groups.groups_id'], name='fk_users_groups_id'),
1055+
PrimaryKeyConstraint('users_id', name='users_pkey')
1056+
)
1057+
1058+
users_id: Mapped[int] = mapped_column(Integer, primary_key=True)
1059+
groups_id: Mapped[Optional[int]] = mapped_column(Integer)
1060+
1061+
group: Mapped[Optional['Group']] = relationship('Group', back_populates='users')
1062+
""",
1063+
)
1064+
1065+
10061066
def test_table_kwargs(generator: CodeGenerator) -> None:
10071067
Table(
10081068
"simple_items",

0 commit comments

Comments
 (0)