MongoEngine / mongoengine

A Python Object-Document-Mapper for working with MongoDB
http://mongoengine.org
MIT License
4.24k stars 1.23k forks source link

Cannot connect to database default : False is not a read preference. #950

Closed GroverChouT closed 2 years ago

GroverChouT commented 9 years ago

I use connect(host='mongodb://username:password@localhost:port/database') in my django settings.py. Run server then got a report:

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

Is this a bug or not? Write in python 3.4.3 with django 1.8 and mongoengine 0.9.0

mseimys commented 9 years ago

Same problem is described on SO. It looks like mongoengine has some issues with the newest version 3.0 of pymongo released just two days ago. Easiest workaround - revert from version 3.0 to 2.8.

GroverChouT commented 9 years ago

@mseimys Thanks

mseimys commented 9 years ago

No problem. Anyway, I wouldn't close this issue just yet - problem is still there, i haven't got enough skills yet to fix it :)

GroverChouT commented 9 years ago

With the progress of refer to: #935 I find it.

tito commented 9 years ago

Got the same problem, i guess Closed is not meant to, as the issue is still present.

orionCMCMSU commented 9 years ago

Got the same problem +1

trbck commented 9 years ago

+1

hamzawaqas-10p commented 9 years ago

+1

MRigal commented 9 years ago

It is fixed in master, so just use mongoengine master (and wait for next release) or use pymongo==2.8. Adding +1 will not help here :)

hamzawaqas-10p commented 9 years ago

I already got it working with 2.8 :P so its not just the matter of +1 ;) @MRigal

lukaspetr commented 9 years ago

+1, please how can I downgrade?

hamzawaqas-10p commented 9 years ago

@lukaspetr Add pymongo==2.8.1 in your requirements.txt

lukaspetr commented 9 years ago

@hamzawaqas-10p thank you. It was also necessary to change the setting to install_requires=['pymongo==2.8.1'], in mongoengine's setup.py

lukaspetr commented 9 years ago

Will this problem be fixed in the next version?

MRigal commented 9 years ago

Since it is in master, for sure :-)

2015-06-04 13:49 GMT+02:00 Petr Lukas notifications@github.com:

Will this problem be fixed in the next version?

— Reply to this email directly or view it on GitHub https://github.com/MongoEngine/mongoengine/issues/950#issuecomment-108857109 .

lukaspetr commented 9 years ago

@MRigal thank you for the piece of information!

GroverChouT commented 9 years ago

Looks like @MRigal fixed it, thank you!

matheusho commented 9 years ago

+1

hamidfzm commented 9 years ago

+1

sit-in commented 9 years ago

2.8 it workd!

srinivasreddy commented 9 years ago

+1

maria commented 8 years ago

+1

hhstore commented 8 years ago

pymongo 3.1 is not work.

now, i have to use pymongo 2.8

thedrow commented 8 years ago

@hhstore I think it's released now. Which version of mongoengine do you have?

riccitensor commented 8 years ago

Still present in MongoDB shell version: 3.2.5.

BurkovBA commented 8 years ago

Got pymongo 3.2.2, mongoengine 0.9, got this bug.

faical-yannick-congo commented 8 years ago

Is this problem solved yet? I want to upgrade my backend to python3.5 and pymongo 3.0.3 is not catching up with you guys for this error. pymongo 2.8 is supported only to python 3.4. Which forces me down to python 3.4.

sathiz1993 commented 7 years ago

I am using django 1.11 , python 3.5, mongoengine 0.13.0, pymongo-3.4.0 same error

jigar1859 commented 7 years ago

+1 mac

SwapneelM commented 6 years ago

OS X High Sierra, mongoengine 0.15.0, pymongo 3.3.2 same error, if it helps get this resolved sooner.

elsampsa commented 6 years ago

Trying to use mongonengine with django, I have to use mongoengine 0.9.0. So now I also have to downgrade pymongo to 2.8. Not good..

ishantanu commented 5 years ago

I am facing this issue as well. Any solution? Downgrading to 2.8 did not solve the issue. Instead, it created another issue.

bagerard commented 5 years ago

@ishantanu Can you clarify the problem? You only observe this with dango, right? Can you provide a code snippet that shows the issue?

ishantanu commented 5 years ago

@bagerard I am observing it with something built with Flask and python. So, after downgrading pymongo to 2.8.1, I am seeing this issue:

Traceback (most recent call last):
  File "toolbox/collect-tweets.py", line 10, in <module>
    from smm import models
  File "/Users/shantanudeshpande/streamcrab/smm/models.py", line 2, in <module>
    import mongoengine
  File "/usr/local/lib/python2.7/site-packages/mongoengine/__init__.py", line 3, in <module>
    from mongoengine import document
  File "/usr/local/lib/python2.7/site-packages/mongoengine/document.py", line 10, in <module>
    from mongoengine.base import (BaseDict, BaseDocument, BaseList,
  File "/usr/local/lib/python2.7/site-packages/mongoengine/base/__init__.py", line 11, in <module>
    from mongoengine.base.metaclasses import *
  File "/usr/local/lib/python2.7/site-packages/mongoengine/base/metaclasses.py", line 9, in <module>
    from mongoengine.queryset import (DO_NOTHING, DoesNotExist,
  File "/usr/local/lib/python2.7/site-packages/mongoengine/queryset/__init__.py", line 3, in <module>
    from mongoengine.queryset.manager import *
  File "/usr/local/lib/python2.7/site-packages/mongoengine/queryset/manager.py", line 2, in <module>
    from mongoengine.queryset.queryset import QuerySet
  File "/usr/local/lib/python2.7/site-packages/mongoengine/queryset/queryset.py", line 4, in <module>
    from mongoengine.queryset.base import (BaseQuerySet, CASCADE, DENY, DO_NOTHING,
  File "/usr/local/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 20, in <module>
    from mongoengine.context_managers import set_write_concern, switch_db
  File "/usr/local/lib/python2.7/site-packages/mongoengine/context_managers.py", line 2, in <module>
    from pymongo.write_concern import WriteConcern
ImportError: No module named write_concern

And, If I use latest version of pymongo, it shows:

Traceback (most recent call last):
  File "toolbox/collect-tweets.py", line 28, in <module>
    models.connect()
  File "/Users/shantanudeshpande/streamcrab/smm/models.py", line 125, in connect
    mongoengine.connect(**conf)
  File "/usr/local/lib/python2.7/site-packages/mongoengine/connection.py", line 272, in connect
    return get_connection(alias)
  File "/usr/local/lib/python2.7/site-packages/mongoengine/connection.py", line 230, in get_connection
    'Cannot connect to database %s :\n%s' % (alias, e))
mongoengine.connection.MongoEngineConnectionError: Cannot connect to database default :
False is not a read preference.

And, If I run with pymongo 2.9, I get this error:

Traceback (most recent call last):
  File "toolbox/collect-tweets.py", line 125, in <module>
    c.run()
  File "toolbox/collect-tweets.py", line 51, in run
    self.get_tweets()
  File "toolbox/collect-tweets.py", line 80, in get_tweets
    self.save(tweet)
  File "toolbox/collect-tweets.py", line 94, in save
    row.save()
  File "/usr/local/lib/python2.7/dist-packages/mongoengine/document.py", line 389, in save
    object_id = self._save_create(doc, force_insert, write_concern)
  File "/usr/local/lib/python2.7/dist-packages/mongoengine/document.py", line 441, in _save_create
    with set_write_concern(collection, write_concern) as wc_collection:
  File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/usr/local/lib/python2.7/dist-packages/mongoengine/context_managers.py", line 248, in set_write_concern
    combined_concerns = dict(collection.write_concern.document.items())

However, on macOS 2.9 works. It does not work on Ubuntu 16.04.

Any idea what might be issue? Mongo is running just fine.

Update:

I made it work by using Mongoengine v 0.10.1. I am not sure why it did not worked with latest version.

bagerard commented 5 years ago

Just to make sure I get this right, the error you have with latest mongoengine and latest pymongo is this one: mongoengine.connection.MongoEngineConnectionError: Cannot connect to database default : False is not a read preference. ? Could you print the values of **conf (on the mongoengine.connect(**conf) call)? Also specify the mongodb version you are using

zentx commented 4 years ago

For those asking if this problem still exists Yeah it still exists, but It worked for me using these versions: Django==2.0.3 mongoengine==0.9.0 pymongo==2.8 python==3.6.4

ShaneHarvey commented 2 years ago

False is not a read preference is the error you'll get if you accidentally pass read_preference=False as a kwarg to MongoClient:

>>> MongoClient(read_preference=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/shane/Library/Python/3.8/lib/python/site-packages/pymongo/mongo_client.py", line 761, in __init__
    dict(common.validate(keyword_opts.cased_key(k), v) for k, v in keyword_opts.items())
  File "/Users/shane/Library/Python/3.8/lib/python/site-packages/pymongo/mongo_client.py", line 761, in <genexpr>
    dict(common.validate(keyword_opts.cased_key(k), v) for k, v in keyword_opts.items())
  File "/Users/shane/Library/Python/3.8/lib/python/site-packages/pymongo/common.py", line 764, in validate
    value = validator(option, value)
  File "/Users/shane/Library/Python/3.8/lib/python/site-packages/pymongo/common.py", line 362, in validate_read_preference
    raise TypeError("%r is not a read preference." % (value,))
TypeError: False is not a read preference.
bagerard commented 2 years ago

Let's close this, it's old and most likely a problem in the argument of connect