Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error When Running Author.save() in MongoAlchemy

It now seems that I can connect to the MongoDB Cluster, but when I try to save then I get the error below:

TypeError: init() got an unexpected keyword argument 'safe'

I have Flask-MongoAlchemy 0.7.1 installed because 0.7.2 doesn't connect at all.

I am following the simple example on the documentation page. In models.py I have the following:

class Author(db.Document):
    name = db.StringField()

I run the following

from myapp import db
from myapp.models import Author
author = Author(name="James")
author.save()

Then I get the error.

I have seen a post on this and have tried adding the required=True to where I declare name. I can confirm the pymongo version is 3.8.0.

I have found that pymongo needs to be 3.8.0 to be able to use the URI with "mongodb+srv", but it needs to be 2.8.0 to enable the safe argument to be passed. The problem is that my code is in python3 and Atlas tells me the connection string has to have "mongodb+srv" so this conflicts with the versions of pymongo. I am not sure if it is even possible to get this working using Flask-MongoAlchemy.

like image 365
OptimusPrime Avatar asked Mar 17 '26 12:03

OptimusPrime


1 Answers

TLDR: Migrate to MongoEngine. It is painless

Came here looking for answers. couldn't find anything online. on GitHub MongoAlchemy says, it is no longer being maintained. If you are looking for answers like me, I had to migrate to MongoEngine

like image 141
Sahith Vibudhi Avatar answered Mar 20 '26 00:03

Sahith Vibudhi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!