Using Alternative Keys

In the example above we could access John Smith's information as follows:

>>> row1 = database['Person'][1]
>>> row2 = database['Person'].row(1)

We could have defined the surname column differently and added it like this instead:

person.addColumn(web.database.object.String(name="surname", unique=True, required=True, key=True))

This defines the surname as a unique, required field. unique means that there cannot be two people with the same surname in the database. If you try to add two people with the same name an Exception will be raised. required means that you must always enter a surname, although in out example, because required is not specified for the firstName column, you would not have to enter a firstName.

Specifying key as True for the surname tells the table that you want to be able to retrieve data from the database based on the surname column rather than the rowid. We can now try the following:

>>> row1 = database['Person']['Smith']
>>> row2 = database['Person'].row(1)
>>> print row1 == row2

You can still access the information by rowid using the row() method.

Any column can be specified as a key but there can only be one column in each table specified as a key. Any column specified as a key must also be specified as unique and required.