marshmallow-mongoengine icon indicating copy to clipboard operation
marshmallow-mongoengine copied to clipboard

SequenceField not saving

Open macfire opened this issue 7 years ago • 1 comments

I have a model that requires an integer for the _id field, so I am using SequenceField to auto-increment.

When creating a new object from the model directly, the object will save and SequenceField auto-increments correct. But when saving from a schema, the SequenceField function is not called.

Not sure if this is a bug, or am I not implementing correctly?

class Book(me.Document):
    _id = me.SequenceField(primary_key=True, 
                           collection_name="counter", 
                           sequence_name="books")
    title = me.StringField()

class BookSchema(ModelSchema):
    class Meta:
        model = Book


""" Examples using Flask-Restful"""

parse = reqparse.RequestParser()
parse.add_argument('title')

# Saving model directly
class Book(Resource):
    def put(self):
        args = parse.parse_args()
        book = Book(title=arg['title'])
        book.save()  
        # will save as insert with the new '_id' retrieved from 'counter' collection


# Saving model using schema
book_schema = BookSchema()

class Book2(Resource):
    def put(self):
        args = parse.parse_args()
        book = book_schema.load(args)
        book.save()  
        # will not save, because `_id` is requirement is not satisfied.
        # that is, it is not automatically calling SequenceField function)

macfire avatar Feb 17 '18 17:02 macfire

Same here! In my case the SequenceField is not a primary key, just a normal sequence number, and it still doesn't work. I have commented the field out for now (although this is not an option for you)

Did you figure something out, since you originally reported your issue?

stratosgear avatar May 26 '18 09:05 stratosgear