DatabaseClassSet.get should return None when id not present

Bug #123592 reported by Michael Hudson-Doyle
4
Affects Status Importance Assigned to Milestone
Launchpad itself
Triaged
Low
Unassigned

Bug Description

I've found at least 4 patterns for what the common SomeDatabaseObjectSet.get(id) methods do when the 'id' is not present:

1. nothing special, and thus raise SQLObjectNotFoundError. This doesn't seem that great an idea, though it's probably ok for methods that never expect to be called for a missing id (the id is never derived from user input, for example).

2. catch SQLObjectNotFoundError and reraise the launchpad specific NotFoundError.

3. return None.

4. ape the dictionary .get() method and return an optional second argument that defaults to None.

This seems a bit of a mess.

While 4 seems the most flexible, I think it's actually wasted effort: if you're after the database row with a particular id, it seems very unlikely that some other (non-None) object will do, and in my grepping I found exactly no code taking advantage of the flexibility of these methods (not even tests). Option 1 has clear disadvantages (exposing implementation details) and so the choice is between 2 and 3.

Steve asked for 3.

Changed in launchpad:
status: New → Confirmed
Revision history for this message
Steve Alexander (stevea) wrote :

We should be returning None.

This makes the code that typically uses these methods simpler, for example web application navigation code.

description: updated
Changed in launchpad:
assignee: nobody → jtv
Revision history for this message
Jeroen T. Vermeulen (jtv) wrote :

SteveA and Kiko suggest a base class ContentObjectSet and interface IContentObjectSet with a consistent interface for common functionality.

Some of the *Set classes allow you to retrieve objects by name, others by id, some both, some by yet other attributes. As a first phase we will make this more explicit by replacing get() with getByName(), getById() etc. (Perhaps a getBy() that takes the identifying attribute as an argument?) We'll also get rid of __getitem__ to avoid conflicts between different classes' expectations.

We could also have a single get() that figures out the right type of search based on the type of its argument, but that may be ambiguous or hide mistakes.

Changed in launchpad-foundations:
assignee: Jeroen T. Vermeulen (jtv) → nobody
Curtis Hovey (sinzui)
Changed in launchpad-foundations:
status: Confirmed → Triaged
importance: Undecided → Low
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.