error while following Tumblelog Application with Flask and MongoEngine
Asked Answered
B

1

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.

Bandit answered 8/4, 2015 at 14:44 Comment(0)
L
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)

Lilla answered 8/4, 2015 at 14:51 Comment(7)
i have tried that too.. app.config["MONGODB_SETTINGS"] = { 'db': "sencha_web_service", 'username': "<username>", "password": "<password>", "port": 27017 }Bandit
@Samuel Littley This is correct - it's a bug in the latest release.Pharmacology
as anyone reported this on github?Show
its an issue with pymongo.. downgrade pymongo to 2.7.2 and now it works fine with mongoengine-0.9.0Alicyclic
A quick look at the MongoDB JIRA came up with this: jira.mongodb.org/browse/PYTHON-719 It looks like going with the pymongo 2.7 branch is the way to go for now.Paba
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.Varicelloid
The Jira ticket indicated they are not going to fix this. What would be the new way of setting this?Lectra

© 2022 - 2025 — McMap. All rights reserved.