Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
GroverChouT opened this issue Apr 9, 2015 · 38 comments
Closed

Comments

@GroverChouT
Copy link

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
Copy link

mseimys commented Apr 9, 2015

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
Copy link
Author

@mseimys Thanks

@mseimys
Copy link

mseimys commented Apr 9, 2015

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
Copy link
Author

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

@tito
Copy link

tito commented May 28, 2015

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

@orionCMCMSU
Copy link

Got the same problem
+1

@trbck
Copy link

trbck commented Jun 1, 2015

+1

1 similar comment
@hamzawaqas-10p
Copy link

+1

@MRigal
Copy link
Member

MRigal commented Jun 3, 2015

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
Copy link

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

@lukaspetr
Copy link

+1, please how can I downgrade?

@hamzawaqas-10p
Copy link

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

@lukaspetr
Copy link

@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
Copy link

Will this problem be fixed in the next version?

@MRigal
Copy link
Member

MRigal commented Jun 4, 2015

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
#950 (comment)
.

@lukaspetr
Copy link

@MRigal thank you for the piece of information!

@GroverChouT
Copy link
Author

Looks like @MRigal fixed it, thank you!

@matheusho
Copy link

+1

1 similar comment
@hamidfzm
Copy link

+1

@sit-in
Copy link

sit-in commented Oct 11, 2015

2.8 it workd!

@srinivasreddy
Copy link
Contributor

+1

1 similar comment
@maria
Copy link

maria commented Nov 22, 2015

+1

@hhstore
Copy link
Contributor

hhstore commented Nov 26, 2015

pymongo 3.1 is not work.

now, i have to use pymongo 2.8

@thedrow
Copy link
Contributor

thedrow commented Dec 8, 2015

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

@riccitensor
Copy link

Still present in MongoDB shell version: 3.2.5.

@BurkovBA
Copy link

BurkovBA commented May 18, 2016

Got pymongo 3.2.2, mongoengine 0.9, got this bug.

@faical-yannick-congo
Copy link

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
Copy link

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

@jigar1859
Copy link

+1 mac

@SwapneelM
Copy link

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

@elsampsa
Copy link

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
Copy link

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

@bagerard bagerard reopened this Mar 30, 2019
@bagerard
Copy link
Collaborator

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

@ishantanu
Copy link

ishantanu commented Mar 31, 2019

@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
Copy link
Collaborator

bagerard commented Mar 31, 2019

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
Copy link

zentx commented Nov 26, 2019

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
Copy link
Contributor

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
Copy link
Collaborator

bagerard commented Sep 8, 2022

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

@bagerard bagerard closed this as completed Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests