Exceptions

exception sheraf.exceptions.ConnectionAlreadyOpened[source]

Bases: sheraf.exceptions.SherafException

Raised when user tries to open an connection when a connection is already opened.

>>> with sheraf.connection():
...     with sheraf.connection():
...         do_amazing_stuff()
Traceback (most recent call last):
    ...
sheraf.exceptions.ConnectionAlreadyOpened: First connection was <Connection at ...> on ... at line ...
exception sheraf.exceptions.EmptyQuerySetUnpackException(message=None)[source]

Bases: sheraf.exceptions.QuerySetUnpackException

Raised when calling get() on QuerySet containing 0 element.

>>> # Fails with zero value
... with sheraf.connection():
...     Cowboy.filter(name="unknown cowboy").get()
Traceback (most recent call last):
...
sheraf.exceptions.EmptyQuerySetUnpackException: Trying to unpack an empty QuerySet
exception sheraf.exceptions.IndexationWarning[source]

Bases: UserWarning

This warning is emitted when you edit or create a model instance which has an outdated indexation table.

exception sheraf.exceptions.InvalidFilterException[source]

Bases: sheraf.exceptions.SherafException

Raised when an invalid QuerySet filter has been called.

>>> class MyModel(sheraf.Model):
...    my_attribute = sheraf.SimpleAttribute()
...
>>> with sheraf.connection():
...    MyModel.filter(foobar=True)
Traceback (most recent call last):
    ...
sheraf.exceptions.InvalidFilterException: MyModel has no attribute foobar
exception sheraf.exceptions.InvalidIndexException[source]

Bases: sheraf.exceptions.SherafException

This exception is raised by read() and read_these() when a parameter is passed and is not a valid index.

>>> class Cowboy(sheraf.Model):
...     table = "cowboy"
...     age = sheraf.IntegerAttribute().index()
...
>>> with sheraf.connection():
...     Cowboy.read(size=4)
Traceback (most recent call last):
...
sheraf.exceptions.InvalidIndexException: 'size' is not a valid index
exception sheraf.exceptions.InvalidOrderException[source]

Bases: sheraf.exceptions.SherafException

Raised when an invalid QuerySet order has been called.

>>> class MyModel(sheraf.Model):
...    table = "my_model"
...    my_attribute = sheraf.SimpleAttribute()
...
>>> with sheraf.connection():
...    MyModel.all().order(foobar=True)
Traceback (most recent call last):
    ...
sheraf.exceptions.InvalidOrderException: MyModel has no attribute foobar
>>> with sheraf.connection():
...    MyModel.all().order(my_attribute=sheraf.ASC) \
...                 .order(my_attribute=sheraf.DESC)  # Raises an InvalidOrderException
Traceback (most recent call last):
    ...
sheraf.exceptions.InvalidOrderException: Some order parameters appeared twice
exception sheraf.exceptions.ModelObjectNotFoundException(model_class, identifier, index_name=None)[source]

Bases: sheraf.exceptions.ObjectNotFoundException

Raised when trying to read an unexisting ModelObject.

Parameters
  • model_class (sheraf.models.BaseModelMetaclass) – Model class of the unfound object

  • object_id (str) – Id of the unfound object

exception sheraf.exceptions.MultipleIndexException[source]

Bases: sheraf.exceptions.InvalidIndexException

This exception is raised by read() when a multiple index is passed as a positionnal argument.

>>> class Cowboy(sheraf.Model):
...     table = "cowboy"
...     name = sheraf.StringAttribute()
...     hobby = sheraf.StringAttribute().index()
...
>>> with sheraf.connection():
...     Cowboy.create(name="George", hobby="nice hats")
...     Cowboy.create(name="Peter", hobby="nice hats")
...     Cowboy.read(hobby="nice hats")
Traceback (most recent call last):
...
sheraf.exceptions.MultipleIndexException: 'hobby' is a multiple index and cannot be used with 'read'
exception sheraf.exceptions.NoDatabaseConnectionException[source]

Bases: sheraf.exceptions.SherafException

Raised when calling read() and there is no connection to the database specified by the attribute database_name.

exception sheraf.exceptions.NotConnectedException[source]

Bases: sheraf.exceptions.SherafException

Raised when trying to handle things in the database while not being connected.

>>> with sheraf.connection():
...     sheraf.attempt(do_amazing_stuff)  # produces amazing stuff
...
>>> sheraf.commit()
Traceback (most recent call last):
    ...
sheraf.exceptions.NotConnectedException
exception sheraf.exceptions.ObjectNotFoundException[source]

Bases: sheraf.exceptions.SherafException

Raised when trying to read an unexisting object.

exception sheraf.exceptions.PrimaryKeyException[source]

Bases: sheraf.exceptions.SherafException

This exception is raised when issues happens with index primary keys.

When creating a model with zero, or several primary indexes.

>>> class Horse(sheraf.AttributeModel):
...     name = sheraf.StringAttribute().index(primary=True)
...     breed = sheraf.StringAttribute().index(primary=True)
...
>>> class Cowboy(sheraf.Model):
...     name = sheraf.StringAttribute()
...     horses = sheraf.IndexedModelAttribute(Horse)
...
>>> with sheraf.connection():
...     george = Cowboy.create(name="George Abitbol")
...     george.horses.create(name="Jolly Jumper", breed="shetland")
Traceback (most recent call last):
...
sheraf.exceptions.PrimaryKeyException: "A model can have only one primary key. 'Horse' has 'name' and 'breed'"
exception sheraf.exceptions.QuerySetUnpackException(message=None)[source]

Bases: sheraf.exceptions.SherafException

Raised when calling get() on QuerySet containing invalid number of elements.

>>> class Cowboy(sheraf.Model):
...     table = "people"
...     name = sheraf.SimpleAttribute()
...
>>> with sheraf.connection():
...     peter = Cowboy.create(name="Peter")
...
...     assert peter == Cowboy.filter(name="Peter").get()
...
>>> # Fails with too many values
... with sheraf.connection():
...     Cowboy.create(name="Peter")
...     Cowboy.create(name="Steven")
...     Cowboy.all().get()
Traceback (most recent call last):
    ...
sheraf.exceptions.QuerySetUnpackException: Trying to unpack more than 1 value from a QuerySet
exception sheraf.exceptions.SameNameForTableException[source]

Bases: sheraf.exceptions.SherafException

Raised when two models have the same “table” attribute.

>>> class FirstModel(sheraf.Model):
...     table = "first_model"
...
>>> class SecondModel(sheraf.Model):
...     table = "first_model"
Traceback (most recent call last):
    ...
sheraf.exceptions.SameNameForTableException: Table named 'first_model' used twice (FirstModel and SecondModel)
exception sheraf.exceptions.SherafException[source]

Bases: Exception

Main sheraf exception.

exception sheraf.exceptions.UniqueIndexException[source]

Bases: sheraf.exceptions.InvalidIndexException

This exception is raised when a value is set twice in an unique index.

>>> class Cowboy(sheraf.Model):
...     table = "cowboy"
...     name = sheraf.StringAttribute().index(unique=True)
...
>>> with sheraf.connection():
...     Cowboy.create(name="George Abitbol")
...     Cowboy.create(name="George Abitbol")
Traceback (most recent call last):
...
sheraf.exceptions.UniqueIndexException: The key 'George Abitbol' is already present in the index 'name'