Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
singhpratibha58 opened this issue Mar 27, 2020 · 4 comments
Labels

Comments

@singhpratibha58
Copy link

singhpratibha58 commented Mar 27, 2020

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')
@sitingren
Copy link
Member

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

@singhpratibha58
Copy link
Author

I have used 0.10.2 driver version.

@sitingren
Copy link
Member

@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?

@singhpratibha58
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants