Jonathan Prieto
Jonathan Prieto

Reputation: 293

Insert row Postgresql with SQL-ALCHEMY with default value

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

Upvotes: 0

Views: 283

Answers (1)

KiDoo Song
KiDoo Song

Reputation: 186

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

Upvotes: 1

Related Questions