OptimusPrime
OptimusPrime

Reputation: 859

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.

Upvotes: 3

Views: 207

Answers (1)

Sahith Vibudhi
Sahith Vibudhi

Reputation: 5195

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

Upvotes: 1

Related Questions