Python - Error in Mongo Engine, Error: Tried to save duplicate unique keys (Duplicate Key Error)

0

Python is trying to save more than one user in Json but it does not accept the introduction of more than one, it always gives the:

  

Tried to save duplicate unique keys (Duplicate Key Error)

Here is the code:

from .model import User, UserSchema
from flask import Blueprint, request
from flask import jsonify

schema = UserSchema()
api = Blueprint('users', __name__)

@api.route('/', methods=['POST'])
def create():
    # Pegar os dados da requisição
    user_json = request.get_json(silent=True)
    if not user_json:
        return "FAIL"

    # Criar uma entidade a partir do JSON
    user, errors = schema.load(user_json)
    if bool(errors):
        return jsonify(errors)
    user.save()
    return "SUCCESS"

@api.route('/', methods=['PATCH'])
def update():
    # Pegar os dados da requisição
    user_json = request.get_json()
    user = User.objects(name=user_json['name']).first()
    schema.update(user, user_json)
    user.save()
    return "SUCESS"

@api.route('/auth', methods=['POST'])
def auth():
    # cria a senha 
    user_json = request.get_json()
    if not user_json:
        return "FAIL"

    user = User.objects(name = user_json['name']).first()
    if not user:
        return "FAIL"
    if user.password == user_json['password']:
        user.save()
        return "SUCCESS"
    else:
        return "FAIL"

@api.route('/', methods=['GET'])
def list():
    # Retorna os usuários do JSON
    list_users = []
    for user in User.objects():
        list_users.append(schema.dump(user))
    return jsonify(list_users)
    
asked by anonymous 29.08.2017 / 21:27

1 answer

1

Solved, it was discovered that I had two variables:

name = StringField (unique = True) password = StringField (unique = True)

We switched to required and worked.

We thank the attention of those who tried to help: D

    
29.08.2017 / 22:09