From d407289e41e017360ee1d9d851ff56031e7e8807 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 22 Sep 2006 17:10:53 +0000 Subject: [PATCH] - ForeignKey reports better error message for column not found - change in verbiage when join conditions are figured out (and fail) --- lib/sqlalchemy/orm/mapper.py | 2 +- lib/sqlalchemy/orm/properties.py | 2 +- lib/sqlalchemy/schema.py | 13 ++++++++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index f8df559022..1539a949cb 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -566,7 +566,7 @@ class Mapper(object): prop.adapt_to_inherited(key, mapper) def __str__(self): - return "Mapper| " + str(id(self)) + "|" + self.class_.__name__ + "|" + (self.entity_name is not None and "/%s" % self.entity_name or "") + (self.local_table and self.local_table.name or str(self.local_table)) + "|is_primary=" + repr(self._is_primary_mapper()) + return "Mapper|" + self.class_.__name__ + "|" + (self.entity_name is not None and "/%s" % self.entity_name or "") + (self.local_table and self.local_table.name or str(self.local_table)) + (not self._is_primary_mapper() and "|non-primary" or "") def _is_primary_mapper(self): """returns True if this mapper is the primary mapper for its class key (class + entity_name)""" diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index 23398f8fef..e05f641a54 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -238,7 +238,7 @@ class PropertyLoader(mapper.MapperProperty): if self.primaryjoin is None: self.primaryjoin = sql.join(self.parent.unjoined_table, self.target).onclause except exceptions.ArgumentError, e: - raise exceptions.ArgumentError("Error determining primary and/or secondary join for relationship '%s' between mappers '%s' and '%s'. You should specify the 'primaryjoin' (and 'secondaryjoin', if there is an association table present) keyword arguments to the relation() function (or for backrefs, by specifying the backref using the backref() function with keyword arguments) to explicitly specify the join conditions. Nested error is \"%s\"" % (self.key, self.localparent, self.mapper, str(e))) + raise exceptions.ArgumentError("Error determining primary and/or secondary join for relationship '%s' between mappers '%s' and '%s'. If the underlying error cannot be corrected, you should specify the 'primaryjoin' (and 'secondaryjoin', if there is an association table present) keyword arguments to the relation() function (or for backrefs, by specifying the backref using the backref() function with keyword arguments) to explicitly specify the join conditions. Nested error is \"%s\"" % (self.key, self.localparent, self.mapper, str(e))) # if the foreign key wasnt specified and theres no assocaition table, try to figure # out who is dependent on who. we dont need all the foreign keys represented in the join, # just one of them. diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 4b6b8d6be0..3cbb7f9e46 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -581,11 +581,14 @@ class ForeignKey(SchemaItem): else: (schema,tname,colname) = m.group(1,2,3) table = Table(tname, parenttable.metadata, mustexist=True, schema=schema) - if colname is None: - key = self.parent - self._column = table.c[self.parent.key] - else: - self._column = table.c[colname] + try: + if colname is None: + key = self.parent + self._column = table.c[self.parent.key] + else: + self._column = table.c[colname] + except KeyError, e: + raise exceptions.ArgumentError("Could not create ForeignKey '%s' on table '%s': table '%s' has no column named '%s'" % (self._colspec, parenttable.name, table.name, e.args[0])) else: self._column = self._colspec -- 2.47.2