Skip to content

Commit d4eaf3f

Browse files
committed
Add another error check to register_model
1 parent d0aa5e7 commit d4eaf3f

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

postgres/__init__.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,20 +219,25 @@ def __str__(self):
219219
"orm models. {} (registered for {}) doesn't fit the bill." \
220220
.format(self.args[0].__name__, self.args[1])
221221

222+
class NoTypeSpecified(Exception):
223+
def __str__(self):
224+
return "You tried to register {} as an orm model, but it has no "\
225+
"typname attribute.".format(self.args[0].__name__)
226+
222227
class NoSuchType(Exception):
223228
def __str__(self):
224-
return "You tried to register an orm Model for typname {}, but no "\
229+
return "You tried to register an orm model for typname {}, but no "\
225230
"such type exists in the pg_type table of your database." \
226231
.format(self.args[0])
227232

228233
class AlreadyRegistered(Exception):
229234
def __str__(self):
230-
return "The Model {} is already registered for the typname {}." \
235+
return "The model {} is already registered for the typname {}." \
231236
.format(self.args[0].__name__, self.args[1])
232237

233238
class NotRegistered(Exception):
234239
def __str__(self):
235-
return "The Model {} is not registered.".format(self.args[0].__name__)
240+
return "The model {} is not registered.".format(self.args[0].__name__)
236241

237242

238243
# The Main Event
@@ -460,7 +465,9 @@ def register_model(self, ModelSubclass):
460465
461466
:param ModelSubclass: the :py:class:`~postgres.orm.Model` subclass to
462467
register with this :py:class:`~postgres.Postgres` instance
463-
:raises: :py:exc:`~postgres.NotAModel`, :py:exc:`~postgres.NoSuchType`,
468+
:raises: :py:exc:`~postgres.NotAModel`,
469+
:py:exc:`~postgres.NoTypeSpecified`,
470+
:py:exc:`~postgres.NoSuchType`,
464471
:py:exc:`~postgres.AlreadyRegistered`
465472
466473
.. note::
@@ -472,6 +479,9 @@ def register_model(self, ModelSubclass):
472479
if not issubclass(ModelSubclass, Model):
473480
raise NotAModel(ModelSubclass)
474481

482+
if getattr(ModelSubclass, 'typname', None) is None:
483+
raise NoTypeSpecified(ModelSubclass)
484+
475485
n = self.one_or_zero( "SELECT count(*) FROM pg_type WHERE typname=%s"
476486
, (ModelSubclass.typname,)
477487
)

0 commit comments

Comments
 (0)