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

Double pluralization of names #262

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/sqlacodegen/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,9 +1002,13 @@ def generate_relationship_name(
RelationshipType.ONE_TO_MANY,
RelationshipType.MANY_TO_MANY,
):
inflected_name = self.inflect_engine.plural_noun(preferred_name)
if inflected_name:
preferred_name = inflected_name
singular_inflected_name = self.inflect_engine.singular_noun(
preferred_name
)
if singular_inflected_name:
preferred_name = self.inflect_engine.plural_noun(
singular_inflected_name
)
else:
inflected_name = self.inflect_engine.singular_noun(preferred_name)
if inflected_name:
Expand Down
106 changes: 106 additions & 0 deletions tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,51 @@ class SimpleItems(Base):
""",
)

@pytest.mark.parametrize("generator", [["use_inflect"]], indirect=True)
def test_onetomany_inflect_singular_table_name(
self, generator: CodeGenerator
) -> None:
Table(
# inflect_engine.singular_noun() should not make this name to 'False':
"simple_item",
generator.metadata,
Column("id", INTEGER, primary_key=True),
Column("container_id", INTEGER),
ForeignKeyConstraint(["container_id"], ["simple_containers.id"]),
)
Table(
"simple_containers",
generator.metadata,
Column("id", INTEGER, primary_key=True),
)

validate_code(
generator.generate(),
"""\
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import declarative_base, relationship

Base = declarative_base()


class SimpleContainer(Base):
__tablename__ = 'simple_containers'

id = Column(Integer, primary_key=True)

simple_item = relationship('SimpleItem', back_populates='container')


class SimpleItem(Base):
__tablename__ = 'simple_item'

id = Column(Integer, primary_key=True)
container_id = Column(ForeignKey('simple_containers.id'))

container = relationship('SimpleContainer', back_populates='simple_item')
""",
)

def test_onetoone(self, generator: CodeGenerator) -> None:
Table(
"simple_items",
Expand Down Expand Up @@ -1882,6 +1927,67 @@ class SimpleItem(Base):
""",
)

@pytest.mark.parametrize("generator", [["use_inflect"]], indirect=True)
def test_use_inflect_plural_double_pluralize(
self, generator: CodeGenerator
) -> None:
Table(
"users",
generator.metadata,
Column("users_id", INTEGER),
Column("groups_id", INTEGER),
ForeignKeyConstraint(
["groups_id"], ["groups.groups_id"], name="fk_users_groups_id"
),
PrimaryKeyConstraint("users_id", name="users_pkey"),
)

Table(
"groups",
generator.metadata,
Column("groups_id", INTEGER),
Column("group_name", Text(50), nullable=False),
PrimaryKeyConstraint("groups_id", name="groups_pkey"),
)

validate_code(
generator.generate(),
(
"""\
from sqlalchemy import Column, ForeignKeyConstraint, Integer, PrimaryKeyConstraint, Text
from sqlalchemy.orm import declarative_base, relationship

Base = declarative_base()


class Group(Base):
__tablename__ = 'groups'
__table_args__ = (
PrimaryKeyConstraint('groups_id', name='groups_pkey'),
)

groups_id = Column(Integer)
group_name = Column(Text(50), nullable=False)

users = relationship('User', back_populates='group')


class User(Base):
__tablename__ = 'users'
__table_args__ = (
ForeignKeyConstraint(['groups_id'], ['groups.groups_id'], \
name='fk_users_groups_id'),
PrimaryKeyConstraint('users_id', name='users_pkey')
)

users_id = Column(Integer)
groups_id = Column(Integer)

group = relationship('Group', back_populates='users')
"""
),
)

def test_table_kwargs(self, generator: CodeGenerator) -> None:
Table(
"simple_items",
Expand Down