e = self.bind
if e is None:
- raise exceptions.InvalidRequestError("This Compiled object is not bound to any Engine or Connection.")
+ raise exceptions.UnboundExecutionError("This Compiled object is not bound to any Engine or Connection.")
return e._execute_compiled(self, multiparams, params)
def scalar(self, *multiparams, **params):
database, but the table doesn't exist.
"""
+class UnboundExecutionError(InvalidRequestError):
+ """SQL was attempted without a database connection to execute it on."""
class AssertionError(SQLAlchemyError):
"""Corresponds to internal state being detected in an invalid state."""
try:
sess = object_mapper(instance).get_session()
except exceptions.InvalidRequestError:
- raise exceptions.InvalidRequestError("Parent instance %s is not bound to a Session, and no contextual session is established; lazy load operation of attribute '%s' cannot proceed" % (mapperutil.instance_str(instance), self.attr.key))
+ raise exceptions.UnboundExecutionError("Parent instance %s is not bound to a Session, and no contextual session is established; lazy load operation of attribute '%s' cannot proceed" % (mapperutil.instance_str(instance), self.attr.key))
return sess.query(self.attr.target_mapper).with_parent(instance, self.attr.key)
try:
session = mapper.get_session()
except exceptions.InvalidRequestError:
- raise exceptions.InvalidRequestError("Instance %s is not bound to a Session, and no contextual session is established; attribute refresh operation cannot proceed" % (instance.__class__))
+ raise exceptions.UnboundExecutionError("Instance %s is not bound to a Session, and no contextual session is established; attribute refresh operation cannot proceed" % (instance.__class__))
state = instance._state
if '_instance_key' in state.dict:
if self.bind is not None:
return self.bind
else:
- raise exceptions.InvalidRequestError("This session is unbound to any Engine or Connection; specify a mapper to get_bind()")
+ raise exceptions.UnboundExecutionError("This session is unbound to any Engine or Connection; specify a mapper to get_bind()")
elif len(self.__binds):
if mapper is not None:
if self.bind is not None:
return self.bind
elif mapper is None:
- raise exceptions.InvalidRequestError("Could not locate any mapper associated with SQL expression")
+ raise exceptions.UnboundExecutionError("Could not locate any mapper associated with SQL expression")
else:
if isinstance(mapper, type):
mapper = _class_mapper(mapper)
mapper = mapper.compile()
e = mapper.mapped_table.bind
if e is None:
- raise exceptions.InvalidRequestError("Could not locate any Engine or Connection bound to mapper '%s'" % str(mapper))
+ raise exceptions.UnboundExecutionError("Could not locate any Engine or Connection bound to mapper '%s'" % str(mapper))
return e
def query(self, mapper_or_class, *addtl_entities, **kwargs):
session = sessionlib.object_session(self.instance)
if session is None:
- raise exceptions.InvalidRequestError("Parent instance %s is not bound to a Session; deferred load operation of attribute '%s' cannot proceed" % (self.instance.__class__, self.key))
+ raise exceptions.UnboundExecutionError("Parent instance %s is not bound to a Session; deferred load operation of attribute '%s' cannot proceed" % (self.instance.__class__, self.key))
query = session.query(localparent)
if not self.optimizing_statement:
try:
session = instance_mapper.get_session()
except exceptions.InvalidRequestError:
- raise exceptions.InvalidRequestError("Parent instance %s is not bound to a Session, and no contextual session is established; lazy load operation of attribute '%s' cannot proceed" % (instance.__class__, self.key))
+ raise exceptions.UnboundExecutionError("Parent instance %s is not bound to a Session, and no contextual session is established; lazy load operation of attribute '%s' cannot proceed" % (instance.__class__, self.key))
q = session.query(prop.mapper).autoflush(False)
if self.path:
def _bind_or_error(schemaitem):
bind = schemaitem.bind
if not bind:
- raise exceptions.InvalidRequestError("This SchemaItem is not connected to any Engine or Connection.")
- return bind
\ No newline at end of file
+ name = schemaitem.__class__.__name__
+ label = getattr(schemaitem, 'fullname',
+ getattr(schemaitem, 'name', None))
+ if label:
+ item = '%s %r' % (name, label)
+ else:
+ item = name
+ if isinstance(schemaitem, MetaData):
+ bindable = "the %s's .bind" % name
+ else:
+ bindable = "this %s's .metadata.bind" % name
+
+ msg = ('The %s is not bound to an Engine or Connection. '
+ 'Execution can not proceed without a database to execute '
+ 'against. Either execute with an explicit connection or '
+ 'assign %s to enable implicit execution.') % (item, bindable)
+ raise exceptions.UnboundExecutionError(msg)
+ return bind
e = self.bind
if e is None:
- raise exceptions.InvalidRequestError("This Compiled object is not bound to any Engine or Connection.")
+ label = getattr(self, 'description', self.__class__.__name__)
+ msg = ('This %s is not bound to an Engine or Connection. '
+ 'Execution can not proceed without a database to execute '
+ 'against. Either execute with an explicit connection or '
+ 'bind the MetaData of the underlying tables to enable '
+ 'implicit execution.') % label
+ raise exceptions.UnboundExecutionError(msg)
return e.execute_clauseelement(self, multiparams, params)
def scalar(self, *multiparams, **params):
for meth in [
metadata.create_all,
- table.exists,
metadata.drop_all,
table.create,
table.drop,
try:
meth()
assert False
- except exceptions.InvalidRequestError, e:
- assert str(e) == "This SchemaItem is not connected to any Engine or Connection."
+ except exceptions.UnboundExecutionError, e:
+ self.assertEquals(
+ str(e),
+ "The MetaData "
+ "is not bound to an Engine or Connection. "
+ "Execution can not proceed without a database to execute "
+ "against. Either execute with an explicit connection or "
+ "assign the MetaData's .bind to enable implicit execution.")
+
+ for meth in [
+ table.exists,
+ # future:
+ #table.create,
+ #table.drop,
+ ]:
+ try:
+ meth()
+ assert False
+ except exceptions.UnboundExecutionError, e:
+ self.assertEquals(
+ str(e),
+ "The Table 'test_table' "
+ "is not bound to an Engine or Connection. "
+ "Execution can not proceed without a database to execute "
+ "against. Either execute with an explicit connection or "
+ "assign this Table's .metadata.bind to enable implicit "
+ "execution.")
+
+ @testing.future
+ def test_create_drop_err2(self):
+ for meth in [
+ table.exists,
+ table.create,
+ table.drop,
+ ]:
+ try:
+ meth()
+ assert False
+ except exceptions.UnboundExecutionError, e:
+ self.assertEquals(
+ str(e),
+ "The Table 'test_table' "
+ "is not bound to an Engine or Connection. "
+ "Execution can not proceed without a database to execute "
+ "against. Either execute with an explicit connection or "
+ "assign this Table's .metadata.bind to enable implicit "
+ "execution.")
@testing.uses_deprecated('//connect')
def test_create_drop_bound(self):
assert e.bind is None
e.execute()
assert False
- except exceptions.InvalidRequestError, e:
- assert str(e) == "This Compiled object is not bound to any Engine or Connection."
+ except exceptions.UnboundExecutionError, e:
+ assert str(e).endswith(
+ 'is not bound to an Engine or '
+ 'Connection. Execution can not proceed without a '
+ 'database to execute against. Either execute with '
+ 'an explicit connection or bind the MetaData of the '
+ 'underlying tables to enable implicit execution.')
finally:
if isinstance(bind, engine.Connection):