Home » Python » python – Unable to save data in database using flask_sqlalchemy error-Exceptionshub

python – Unable to save data in database using flask_sqlalchemy error-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I am new at flask and I am trying to save some data in my database but its not working and I do not know why. It would be appreciated and really great if anyone can help me because this problem is like killing me.
If you think that i should add something about the code or more explanations please response

This is the output:

Traceback (most recent call last):
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\flask\app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\flask\app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\flask\app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\flask\app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\big_projects\blockchain_project\new_blockchain\node\routes.py", line 50, in register
    db.session.commit()
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\orm\scoping.py", line 162, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\orm\session.py", line 1036, in commit
    self.transaction.commit()
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\orm\session.py", line 503, in commit
    self._prepare_impl()
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\orm\session.py", line 482, in _prepare_impl
    self.session.flush()
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\orm\session.py", line 2479, in flush
    self._flush(objects)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\orm\session.py", line 2617, in _flush
    transaction.rollback(_capture_exception=True)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\util\compat.py", line 153, in reraise
    raise value
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\orm\session.py", line 2577, in _flush
    flush_context.execute()
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 422, in execute
    rec.execute(self)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 589, in execute
    uow,
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\orm\persistence.py", line 245, in save_obj
    insert,
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\orm\persistence.py", line 1137, in _emit_insert_statements
    statement, params
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\engine\base.py", line 982, in execute
    return meth(self, multiparams, params)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\sql\elements.py", line 293, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\engine\base.py", line 1101, in _execute_clauseelement
    distilled_params,
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\engine\base.py", line 1250, in _execute_context
    e, statement, parameters, cursor, context
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\engine\base.py", line 1476, in _handle_dbapi_exception
    util.raise_from_cause(sqlalchemy_exception, exc_info)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\engine\base.py", line 1246, in _execute_context
    cursor, statement, parameters, context
  File "C:\Users\DZiml\.conda\envs\zims\lib\site-packages\sqlalchemy\engine\default.py", line 588, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: userDB.public_key
[SQL: INSERT INTO "userDB" (email, password, private_key, public_key) VALUES (?, ?, ?, ?)]
[parameters: ('[email protected]', '$2b$12$x1FHhAviMLdnaixFIWMZXu1GlRFxXkCG0gRpsjk88SLIJ8LPDhgn.', '3082025d02', '30819f300d')]
(Background on this error at: http://sqlalche.me/e/gkpj)

This is the lines i run(at node\routes.py):

user = UserDB(email=form.email.data, password=hashed_password, private_key=wallet.private_key[0:10], public_key=wallet.public_key[0:10])
db.session.add(user)
db.session.commit()

node__init__.py:

from flask import Flask
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt


app = Flask(__name__)
CORS(app)
app.config['SECRET_KEY'] = '9aad38e8b45239fc1d0124234a4dbec0'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)

from node import routes

node\models.py:

from node import db

class UserDB(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(100), unique=True, nullable=False)
    password = db.Column(db.String(60), nullable=False)
    private_key = db.Column(db.String(1216), unique=True, nullable=False)
    public_key = db.Column(db.String(324), unique=True, nullable=False)

    def __repr__(self):
        return f"User('{self.public_key}')"

class BlockchainDB(db.Model):
    index = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
    previous_hash = db.Column(db.String(64), nullable=False)
    current_hash = db.Column(db.String(64), nullable=False)
    proof = db.Column(db.String(1024), unique=True, nullable=False)
    timestamp = db.Column(db.String(324), unique=True, nullable=False)

    def __repr__(self):
        return f"User('{self.index}')"
How to&Answers: