0

I have some table in postgresql that have an ID that is automatically created thank to sequences. But now that I am using flask with SQLALCHEMY, when I try to insert a value, It says that I have a parameter left because I am not passing the ID value because it is postgres that will make it. I think that I should change the model but don´t know how.

I join you, my model:

class User(UserMixin, db.Model):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True) # primary keys are required by SQLAlchemy
    email = db.Column(db.String(100), unique=True)
    password = db.Column(db.String(100))
    name = db.Column(db.String(1000))
    permitido = db.Column(db.BOOLEAN)
    admin = db.Column(db.BOOLEAN)
    superuser = db.Column(db.BOOLEAN)

    def __init__(self, id, email, password, name, permitido, admin, superuser):
        self.id = id

        self.email = email
        self.password = password
        self.name = name
        self.permitido = permitido
        self.admin = admin
        self.superuser = superuser

And here is my insert code:

@app.route('/insertinternalusers', methods=['POST'])
def insertinternalusers():
    if request.method == 'POST':
        email = request.form['email']
        password = request.form['password']
        name = request.form['name']

        if request.form.get('permitido'):
            permitido = True
        else:
            permitido = False

        if request.form.get('admin'):
            admin = True
        else:
            admin = False

        superuser = False

        my_data = User(email, password, name, permitido, admin, superuser)
        db.session.add(my_data)
        db.session.commit()

        flash("¡Se agrego satisfactoriamente un Usuario!")

        return redirect(url_for('show_internalusers'))

Thank you,

Jonathan Prieto

1 Answer 1

1

User classes take the 'id' argument.

Called like this

 my_data = User(None, email, password, name, permitido, admin, superuser)

or

def __init__(self, email, password, name, permitido, admin, superuser):
    self.email = email
    self.password = password
    self.name = name
    self.permitido = permitido
    self.admin = admin
    self.superuser = superuser
Sign up to request clarification or add additional context in comments.

1 Comment

Please refer to the corresponding document for the contents of the sequence. docs.sqlalchemy.org/en/13/core/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.