benoitc/couchdbkit

Weirdness of "if not db:" in an empty database

Closed this issue · 0 comments

If you create a new database with no documents, CouchDBKit will not work happily. The problem is this:

  1. I'm calling MyDocument.get(my_id, db=my_db) to see if an object exists before I create it.
  2. The Document.get() classmethod take an optional database. The first thing the method does is check "if not db:" so it can try to get the db off the document if necessary.
  3. "if not db:" turns into "Database.nonzero()", which in turn turns into "Database.len != 0" which really asks "Does the database have more than zero objects?"
  4. This returns False, even though my db is actually good and happy. It moves on to check the document that I passed in for a db, which it doesn't have, and then barfs.

I would submit a patch, as I assume the correct behavior is for Document.get() to do "if db is None", but I don't know if there are places in the code that depend on this (IMO broken) behavior.