Search code examples
databaseflasktriggerssqlplusauto-increment

trying to autoincrement primarykey using trigger in flask and storing in sqlplus oracle 10g


'''@app.route("/indexf",methods=["POST","GET"])
def signup():
     conn=cx_Oracle.connect(user="SYSTEM",password="bnm")
     cur= conn.cursor()
     a1=""""create sequence fid_seq"""
     ass=""" create trigger tfarmer
          before insert on farmeruser
           for each row
           begin
           select fid_seq.nextval
          into :new.fid
           from dual
           end;
           /"""
     
   
     
     if request.method == "POST":
            
            fname=request.form.get('fname')
            email=request.form.get('email')
            password=request.form.get('password')
           
            
          
            encpassword=generate_password_hash(password)
            

         
            execute="""INSERT INTO farmeruser VALUES (:fid,:fname,:email,:password)"""
            cur.execute(execute, {'fid':fid,'fname':fname, 'email':email, 'password':encpassword})
     
            conn.commit()

     return render_template('indexf.html') 
'''

here i am trying to get fname,email,password from user and autoincrement fid which is primary key using triggers .but i dont know how to pass fid in flask to autoincrement


Solution

  • Interesting. 10g, really? Are you working for a museum? :-) Current production version is 19c, 10g is 20 years old ... Sorry for the cheap joke.

    So your trigger works, if you want to set the value for fid explicitly (this is how I understand your question) you must change the trigger:

    ...
    begin
    IF :new.fid IS NULL THEN
       SELECT fid_seq.nextval INTO :new.fid FROM dual;
       END IF;
    END;
    

    If you decide to change to a more recent Oracle version you can do

    :new.fid:= fid_seq.nextval;
    

    instead of the SELECT statement.