Skip to content

Commit d906e0f

Browse files
committed
Fixed TypeError when inflect couldn't determine a name for a relationship from the remote table name
1 parent 880b50f commit d906e0f

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Version history
22
===============
33

4+
1.1.1
5+
-----
6+
7+
* Fixed TypeError when inflect could not determine the singular name of a table for a many-to-1 relationship
8+
9+
410
1.1.0
511
-----
612

sqlacodegen/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = '1.1.0.post1'
1+
version = '1.1.1.pre1'

sqlacodegen/codegen.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,10 @@ def __init__(self, source_cls, target_cls, constraint, inflect_engine):
355355

356356
colname = constraint.columns[0]
357357
tablename = constraint.elements[0].column.table.name
358-
self.preferred_name = inflect_engine.singular_noun(tablename) if not colname.endswith('_id') else colname[:-3]
358+
if not colname.endswith('_id'):
359+
self.preferred_name = inflect_engine.singular_noun(tablename) or tablename
360+
else:
361+
self.preferred_name = colname[:-3]
359362

360363
# Add uselist=False to One-to-One relationships
361364
if any(isinstance(c, (PrimaryKeyConstraint, UniqueConstraint)) and

test/test_codegen.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,44 @@ class SimpleItem(Base):
509509
other_item = relationship('OtherItem', uselist=False)
510510
""")
511511

512+
def test_onetomany_noinflect(self):
513+
Table(
514+
'oglkrogk', self.metadata,
515+
Column('id', INTEGER, primary_key=True),
516+
Column('fehwiuhfiwID', INTEGER),
517+
ForeignKeyConstraint(['fehwiuhfiwID'], ['fehwiuhfiw.id']),
518+
)
519+
Table(
520+
'fehwiuhfiw', self.metadata,
521+
Column('id', INTEGER, primary_key=True)
522+
)
523+
524+
eq_(self.generate_code(), """\
525+
# coding: utf-8
526+
from sqlalchemy import Column, ForeignKey, Integer
527+
from sqlalchemy.orm import relationship
528+
from sqlalchemy.ext.declarative import declarative_base
529+
530+
531+
Base = declarative_base()
532+
metadata = Base.metadata
533+
534+
535+
class Fehwiuhfiw(Base):
536+
__tablename__ = 'fehwiuhfiw'
537+
538+
id = Column(Integer, primary_key=True)
539+
540+
541+
class Oglkrogk(Base):
542+
__tablename__ = 'oglkrogk'
543+
544+
id = Column(Integer, primary_key=True)
545+
fehwiuhfiwID = Column(ForeignKey('fehwiuhfiw.id'))
546+
547+
fehwiuhfiw = relationship('Fehwiuhfiw')
548+
""")
549+
512550
def test_manytomany(self):
513551
Table(
514552
'simple_items', self.metadata,

0 commit comments

Comments
 (0)