vertica-python icon indicating copy to clipboard operation
vertica-python copied to clipboard

[Flask integration] Vertica-python integration with flask is allowing to insert duplicate value for primary key/unique column

Open singhpratibha58 opened this issue 5 years ago • 4 comments

Vertica-python integration with flask framework is allowing to insert duplicate value for primary key column.However on browser it was not showing duplicates but in database records are inserted.

import os
from flask import Flask
from flask import render_template
from flask import request
from flask import redirect
from flask_sqlalchemy import SQLAlchemy
 
app = Flask(__name__)
app.config.update(
    SECRET_KEY='',
    SQLALCHEMY_DATABASE_URI=vertica+vertica_python://<dbuser>:<dbpassword>@<dbmachineip>:<dbport>/<dbname>,
    SQLALCHEMY_TRACK_MODIFICATIONS= False
    )
 
db = SQLAlchemy(app)
 
class Employee(db.Model):
    emp_name = db.Column(db.String(80), unique=True, nullable=False, primary_key=True)
 
    def __repr__(self):
        return "<Employeename: {}>".format(self.emp_name)
 
 
@app.route("/", methods=["GET", "POST"])
def home():
    employees = None
    if request.form:
        try:
            employee = Employee(emp_name=request.form.get("emp_name"))
            db.session.add(employee)
            db.session.commit()
        except Exception as e:
            print("Failed to show employee name ")
            print(e)
    employees = Employee.query.all()
    return render_template("home.html", employees=employees)
 
 
@app.route("/update", methods=["POST"])
try:
        new_name = request.form.get("newname")
        old_name = request.form.get("oldname")
        employees = Employee.query.filter_by(emp_name=old_name).first()
        employees.emp_name = new_name
        db.session.commit()
    except Exception as e:
        print("Couldn't update employee name")
        print(e)
    return redirect("/")
 
 
@app.route("/delete", methods=["POST"])
def delete():
    emp_name = request.form.get("delname")
    employees = Employee.query.filter_by(emp_name=emp_name).first()
    db.session.delete(employees)
    db.session.commit()
    return redirect("/")
 
 
if __name__ == "__main__":
    db.create_all()
    app.run(host='ServerHostIP')

singhpratibha58 avatar Mar 27 '20 07:03 singhpratibha58

Please provide the version info for those packages you installed. Thanks.

sitingren avatar Mar 30 '20 09:03 sitingren

I have used 0.10.2 driver version.

singhpratibha58 avatar Mar 31 '20 08:03 singhpratibha58

@singhpratibha58 I mean what packages have you installed? Such as Flask, and SQLAlchemy. I guess you also used a SQLAlchemy Vertica dialect, where does it come from?

sitingren avatar Mar 31 '20 10:03 sitingren

I have these versions Package Version


Flask 1.1.1 Flask-Migrate 2.5.2 Flask-SQLAlchemy 2.4.1 Flask-WTF 0.14.3 SQLAlchemy 1.3.13 sqlalchemy-vertica 0.0.5 sqlalchemy-vertica-python 0.4.4 vertica-python 0.10.2 I used pip command to setup this.

singhpratibha58 avatar Apr 01 '20 07:04 singhpratibha58