8

I am following tumbleblog application here

my __init__.py:

from flask import Flask
from flask.ext.mongoengine import MongoEngine

app = Flask(__name__)
app.config["MONGODB_SETTINGS"] = {'DB': "sencha_web_service", 'username': "<username>", "password": "<password>"}
app.config["SECRET_KEY"] = "KeepThisS3cr3t"

db = MongoEngine(app)

if __name__ == '__main__':
    app.run()

I get the error:

mongoengine.connection.ConnectionError: Cannot connect to database default :
False is not a read preference.

I tried passing in "alias"="default" in app.config["MONGODB_SETTINGS"] but still getting the same error.

austin
  • 5,816
  • 2
  • 32
  • 40
Nidhin Bose J.
  • 1,092
  • 15
  • 28

1 Answers1

11

In your MONGODB_SETTINGS dictionary, the key for the database name should be 'db', not 'DB' (i.e. all lowercase).

The error you're getting is because the MongoEngine extension cannot find the 'db' entry in your configuration, and so uses 'default' as the database name.

Edit

Upon further inspection, it seems this is a bug somewhere in (Flask-)MongoEngine (or possible pymongo) where the default value of read_preference in mongoengine.connect is False instead of an actual read preference, and is not transformed to the actual default in pymongo

If you add

from pymongo import read_preferences

to your imports and

'read_preference': read_preferences.ReadPreference.PRIMARY

to your config dictionary, it should work (that's the default read_preference in pymongo)

Samuel Littley
  • 724
  • 7
  • 17
  • i have tried that too.. app.config["MONGODB_SETTINGS"] = { 'db': "sencha_web_service", 'username': "", "password": "", "port": 27017 } – Nidhin Bose J. Apr 08 '15 at 15:08
  • @Samuel Littley This is correct - it's a bug in the latest release. – okoboko Apr 08 '15 at 15:43
  • as anyone reported this on github? – dpgaspar Apr 09 '15 at 17:30
  • 1
    its an issue with pymongo.. downgrade pymongo to 2.7.2 and now it works fine with mongoengine-0.9.0 – gsuresh92 Apr 11 '15 at 11:28
  • A quick look at the MongoDB JIRA came up with this: https://jira.mongodb.org/browse/PYTHON-719 It looks like going with the pymongo 2.7 branch is the way to go for now. – austin Apr 15 '15 at 01:50
  • In my case I just had `mongoengine>=0.8.4,<0.9` in my `requirements.txt` file, and pymongo was being pulled in implicitly. Adding `pymongo>=2.7,<3.0` to that file fixed the issue. – z0r Apr 15 '15 at 04:15
  • The Jira ticket indicated they are not going to fix this. What would be the new way of setting this? – ericso May 15 '15 at 19:53