Solved: sqlalchemy session.query...first() crashes if no data found - python

I always get an excessive traceback error, when querying the database finds no result. Why does this query not simply return None?
userExpireDate = session.query(PayingUser.expireDate).filter(PayingUser.mail == mailaddress).first()
This gives this horror error message when the mailaddress can't be found:
Traceback (most recent call last):
File "/Volumes/Mac HD/Benutzer/oliver/PycharmProjects/ErsterTest/Praeprocessor.py", line 274, in <module>
main()
File "/Volumes/Mac HD/Benutzer/oliver/PycharmProjects/ErsterTest/Praeprocessor.py", line 263, in main
userExpireDate = checkUser(mail)
File "/Volumes/Mac HD/Benutzer/oliver/PycharmProjects/ErsterTest/Praeprocessor.py", line 57, in checkUser
userExpireDate = session.query(PayingUser.expireDate).filter(PayingUser.mail == sender).first()
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 3240, in first
ret = list(self[0:1])
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 3018, in __getitem__
return list(res)
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 3341, in __iter__
self.session._autoflush()
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1588, in _autoflush
util.raise_from_cause(e)
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
raise value
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1577, in _autoflush
self.flush()
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2470, in flush
self._flush(objects)
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2608, in _flush
transaction.rollback(_capture_exception=True)
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
raise value
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2568, in _flush
flush_context.execute()
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
rec.execute(self)
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
uow,
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
insert,
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 1137, in _emit_insert_statements
statement, params
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 988, in execute
return meth(self, multiparams, params)
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1107, in _execute_clauseelement
distilled_params,
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1253, in _execute_context
e, statement, parameters, cursor, context
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1473, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1249, in _execute_context
cursor, statement, parameters, context
File "/Volumes/Mac HD/Benutzer/oliver/ErsterTest/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 580, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely)
(sqlite3.OperationalError) no such table: mails
[SQL: INSERT INTO mails (sender, subject, received, answer, text, html, category, count, danger) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)]
[parameters: ('anemailaddress#gmx.at', 'An subject', '2019-10-15 23:59:11.219968', 2, '\r\n\r\n> Anfang der weitergeleiteten Nachricht:\r\n>\r\n> Von: Name <anemailaddress#gmx.at>\r\n> Betreff: An subject (87 characters truncated) ... > Kopie: MrX <anemailaddress#gmx.at>\r\n> Antwort an: MrX <anemailaddress#gmx.at>\r\n>\r\n> Ein weiteres Mail\r\n\r\n', '<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space ... (2865 characters truncated) ... ass=""></span></div><br class=""><div class=""><div class="">Ein weiteres Mail<br class=""></div></div></blockquote></div><br class=""></body></html>', '', 0, 0)]
(Background on this error at: http://sqlalche.me/e/e3q8)
Why?
Interestingly enough it gives no Error when I do this:
for userExpireDate in session.query(PayingUser.expireDate).filter(PayingUser.mail == sender):
But I'd like to detect if there was no result. And if I try it this way (I know that's not elegant but nevertheless):
i = 0
for userExpireDate in session.query(PayingUser.expireDate).filter(PayingUser.mail == sender):
i += 1
...
if i == 0:
print('No user in database')
then I again get a horribly long traceback error.
Here is the model definition in another file called: payingUser.py
from sqlalchemy import Column, String, Integer, DateTime
from base import Base
from datetime import datetime
class PayingUser(Base):
__tablename__ = 'payingUsers'
mailaddress = Column(String, primary_key=True, unique=True)
firstName = Column(String)
lastName = Column(String)
expireDate = Column(DateTime)
def __init__(self, mailaddress, firstName, lastName):
self.mailaddress = mailaddress
self.firstName = firstName
self.lastName = lastName
self.expireDate = datetime.now()
What's happening here? Can anybody help? Yes, I'm new to sqlalchemy.
There is another table that's not used in this query, but kind of seems to appear in the traceback.
from sqlalchemy import Column, String, Integer, DateTime, Text, Table
from datetime import datetime
from base import Base
class Mail(Base):
__tablename__ = 'mails'
mail_id = Column(Integer, primary_key=True)
sender = Column(String(100))
subject = Column(String)
received = Column(DateTime, nullable=True)
answer = Column(Integer)
text = Column(Text)
html = Column(Text)
def __init__(self):
self.sender = ''
self.subject = ''
self.received = 0
self.answer = 0
self.text = ''
self.html = ''
Thanks!!

I am an idiot!
The reason for my error was, that I had the model definitions in separated files and I imported one of them AFTER the create_all(engine) statement. So I practically did this:
from testingUser import TestingUser
Base.metadata.create_all(engine)
session = Session()
from mail import Mail
Making it correct solved all my problems.
from testingUser import TestingUser
from mail import Mail
Base.metadata.create_all(engine)
session = Session()
Me butthead!!! This took me 3 days. Arghh.

Related

SQL Alchemy - Error UndefinedColumn while it exists

Here is my code:
class Handover_Point(Base):
__tablename__ = 'r_handover_point'
id = Column(UUID(as_uuid=True), primary_key=True)
point = Column(String(255))
name = Column(String(255))
short_id = Column(String(255))
address = Column(String(255))
geo = Column(Geometry('POINT', spatial_index=False))
pic_name = Column(String(255))
mobile = Column(String(255))
email = Column(String(255))
is_active = Column(Boolean)
is_hub = Column(Boolean)
__table_args__ = (
Index('idx_hp_geo', 'geo', postgresql_using='gist'),
{'schema': 'routing'},
)
rule = Session.query(Handover_Point.name,
Handover_Point.pic_name,
Handover_Point.address,
Handover_Point.is_hub).all()
But when I run it, I go this error:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedColumn) column r_handover_point.is_hub does not exist
LINE 1: ...oint.address AS routing_r_handover_point_address, routing.r
This is the image of my database with all columns
Here is my code to check if the column is_hub exists
from sqlalchemy import inspect
mapper = inspect(Handover_Point)
for column in mapper.attrs:
print(column.key)
I got this
id
point
name
short_id
address
geo
pic_name
mobile
email
is_active
is_hub
Traceback Error:
Traceback (most recent call last):
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1276, in _execute_context
self.dialect.do_execute(
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\default.py", line 593, in do_execute
cursor.execute(statement, parameters)
psycopg2.errors.UndefinedColumn: column r_handover_point.is_hub does not exist
LINE 1: ...oint.address AS routing_r_handover_point_address, routing.r_...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\test.py", line 39, in <module>
rule = Session.query(Handover_Point.name,
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\orm\query.py", line 3373, in all
return list(self)
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\orm\query.py", line 3535, in __iter__
return self._execute_and_instances(context)
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\orm\query.py", line 3560, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1011, in execute
return meth(self, multiparams, params)
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\sql\elements.py", line 298, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1124, in _execute_clauseelement
ret = self._execute_context(
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1316, in _execute_context
self._handle_dbapi_exception(
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1510, in _handle_dbapi_exception
util.raise_(
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\util\compat.py", line 182, in raise_
raise exception
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1276, in _execute_context
self.dialect.do_execute(
File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\default.py", line 593, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedColumn) column r_handover_point.is_hub does not exist
LINE 1: ...oint.address AS routing_r_handover_point_address, routing.r_...
^
[SQL: SELECT routing.r_handover_point.name AS routing_r_handover_point_name, routing.r_handover_point.pic_name AS routing_r_handover_point_pic_name, routing.r_handover_point.address AS routing_r_handover_point_address, routing.r_handover_point.is_hub AS routing_r_handover_point_is_hub
FROM routing.r_handover_point]
(Background on this error at: http://sqlalche.me/e/13/f405)

Flask SQLAlchemy Won't Create Record (DateTimeField)

I'm putting together a CRM, and I'm having a little trouble. Everything has been working just fine, until I tried passing a time with a DateTimeField with SQLAlchemy. Prior to adding the time field, the record would create and add to the database without any issues. After, however, I the record simply wouldn't create. The page would reload, and I'd have no validation errors. I've been plugging away at it, and I've finally received an error. All of the data is passing through fine, except the time. The time is being fed through as <wtforms.fields.core.DateTimeField object at 0x10fe67210>. How do I get around this issue? I feel like the solution is simple, and I'm going to feel dumb for not seeing it sooner. Your help is greatly appreciated!
Here is the entire error log:
sqlalchemy.exc.StatementError
sqlalchemy.exc.StatementError: (builtins.TypeError) SQLite DateTime type only accepts Python datetime and date objects as input.
[SQL: INSERT INTO tours (customer_fname, customer_lname, customer_phone, partner_fname, partner_lname, partner_phone, region, tour_date, tour_time, gift, timestamp, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]
[parameters: [{'user_id': 1, 'partner_phone': 'jkljlkj', 'tour_time': <wtforms.fields.core.DateTimeField object at 0x10fe67210>, 'partner_lname': 'jkljlk', 'region ... (34 characters truncated) ... ': 'kljklj', 'gift': 'Cruise', 'customer_lname': 'lkjlk', 'customer_phone': 'jkljl', 'partner_fname': 'jlkjlk', 'timestamp': None, 'tour_date': None}]]
Traceback (most recent call last)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1173, in _execute_context
context = constructor(dialect, self, conn, *args)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 808, in _init_compiled
param.append(processors[key](compiled_params[key]))
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/dialects/sqlite/base.py", line 759, in process
"SQLite DateTime type only accepts Python "
The above exception was the direct cause of the following exception:
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask_login/utils.py", line 272, in decorated_view
return func(*args, **kwargs)
File "/Users/brandoncarr/Desktop/BlueSkyAdmin/app/routes.py", line 68, in new_tour
db.session.commit()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 162, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1036, in commit
self.transaction.commit()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 503, in commit
self._prepare_impl()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 482, in _prepare_impl
self.session.flush()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2479, in flush
self._flush(objects)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2617, in _flush
transaction.rollback(_capture_exception=True)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
raise value
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2577, in _flush
flush_context.execute()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
rec.execute(self)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
uow,
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
insert,
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 1137, in _emit_insert_statements
statement, params
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 982, in execute
return meth(self, multiparams, params)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 293, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1101, in _execute_clauseelement
distilled_params,
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1176, in _execute_context
e, util.text_type(statement), parameters, None, None
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1476, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1173, in _execute_context
context = constructor(dialect, self, conn, *args)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 808, in _init_compiled
param.append(processors[key](compiled_params[key]))
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/dialects/sqlite/base.py", line 759, in process
"SQLite DateTime type only accepts Python "
My view:
#app.route('/tours/new', methods=['GET', 'POST'])
#login_required
def new_tour():
form = ToursForm()
if form.validate_on_submit():
tours = Tours(customer_fname=form.customer_fname.data, customer_lname=form.customer_lname.data,
partner_fname=form.partner_fname.data, partner_lname=form.partner_lname.data,
customer_phone=form.customer_phone.data, partner_phone=form.partner_phone.data,
region=form.region.data, gift=form.gift.data, tour_time=form.tour_time, author=current_user)
db.session.add(tours)
db.session.commit()
flash('Tour has been created', 'success')
return redirect(url_for('tours'))
return render_template('create-tour.html', title='Create A New Tour', form=form)
My model:
class Tours(db.Model):
id = db.Column(db.Integer, primary_key=True)
customer_fname = db.Column(db.String(50))
customer_lname = db.Column(db.String(50))
customer_phone = db.Column(db.String(140))
partner_fname = db.Column(db.String(100))
partner_lname = db.Column(db.String(100))
partner_phone = db.Column(db.String(100))
region = db.Column(db.String(140))
tour_date = db.Column(db.DateTime())
tour_time = db.Column(db.DateTime, default=datetime.utcnow)
gift = db.Column(db.String())
timestamp = db.Column(db.DateTime())
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
def __repr__(self):
return '<Tours {}>'.format(self.customer_fname, self.customer_lname, self.customer_phone,
self.partner_fname, self.partner_lname, self.partner_phone, self.region, self.tour_date,
self.tour_time, self.gift, self.timestamp)
My form:
class ToursForm(FlaskForm):
customer_fname = StringField('Customer First Name', validators=[DataRequired()])
customer_lname = StringField('Customer Last Name', validators=[DataRequired()])
customer_phone = StringField('Phone Number', validators=[DataRequired()])
partner_fname = StringField('Partner First Name', validators=[DataRequired()])
partner_lname = StringField('Partner Last Name', validators=[DataRequired()])
partner_phone = StringField('Phone Number', validators=[DataRequired()])
# tour_date = DateTimeField('Tour Date')
tour_time = DateTimeField('Tour Time', format='%I:%M %p')
gift = SelectField('Gift Selected', choices=[('Cruise', 'Cruise'), ('Hotel Suite', 'Hotel Suite'), ('Airfare', 'Airfare'), ('Safari', 'Safari')])
region = SelectField('Region', choices=[('Select Region', 'Select Region'), ('Oceanside', 'Oceanside'), ('Scottsdale', 'Scottsdale'), ('Granite Bay', 'Granite Bay'), ('Laguna Hills', 'Laguna Hills')])
submit = SubmitField('Add Tour')
As with all the other fields, access tour_time field's data. Now you are passing the field itself as value.
tour_time=form.tour_time
should be
tour_time=form.tour_time.data

Can't set uuid primary field in postgresql table flask-sqlalchemy

I am creating a backend with multiple tables have user id as the primary field with is of UUID type in the postgresql database. But I am getting the following error:
Traceback (most recent call last):
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1179, in _execute_context
context = constructor(dialect, self, conn, *args)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 743, in _init_compiled
for key in compiled_params
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 743, in <genexpr>
for key in compiled_params
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 487, in process
value = _python_UUID(value)
File "/usr/lib64/python3.7/uuid.py", line 157, in __init__
hex = hex.replace('urn:', '').replace('uuid:', '')
AttributeError: 'UUID' object has no attribute 'replace'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/flask/app.py", line 1832, in full_dispatch_request
rv = self.dispatch_request()
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/flask/app.py", line 1818, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/flask_restful/__init__.py", line 458, in wrapper
resp = resource(*args, **kwargs)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/flask_restful/__init__.py", line 573, in dispatch_request
resp = meth(*args, **kwargs)
File "/mnt/home/Programming/Projects/Social Network/Server/src/new_user.py", line 17, in post
self.database_helper(args)
File "/mnt/home/Programming/Projects/Social Network/Server/src/new_user.py", line 26, in database_helper
db.session.commit()
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 162, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1026, in commit
self.transaction.commit()
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 493, in commit
self._prepare_impl()
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 472, in _prepare_impl
self.session.flush()
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2458, in flush
self._flush(objects)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2596, in _flush
transaction.rollback(_capture_exception=True)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 129, in reraise
raise value
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2556, in _flush
flush_context.execute()
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
rec.execute(self)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
uow,
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
insert,
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 1066, in _emit_insert_statements
c = cached_connections[connection].execute(statement, multiparams)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 988, in execute
return meth(self, multiparams, params)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1107, in _execute_clauseelement
distilled_params,
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
e, util.text_type(statement), parameters, None, None
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1466, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 383, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 128, in reraise
raise value.with_traceback(tb)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1179, in _execute_context
context = constructor(dialect, self, conn, *args)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 743, in _init_compiled
for key in compiled_params
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 743, in <genexpr>
for key in compiled_params
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 487, in process
value = _python_UUID(value)
File "/usr/lib64/python3.7/uuid.py", line 157, in __init__
hex = hex.replace('urn:', '').replace('uuid:', '')
sqlalchemy.exc.StatementError: (builtins.AttributeError) 'UUID' object has no attribute 'replace'
Here is my model:
from database.shared_db import db
from sqlalchemy.dialects import postgresql
from database.shared_bycrypt import bcrypt
class AuthenticationModel(db.Model):
user_id = db.Column('UserId', postgresql.UUID, primary_key = True)
email = db.Column('Email', postgresql.TEXT, unique = False, nullable=False)
phone_no = db.Column('PhoneNo', postgresql.TEXT, unique = False, nullable=False)
password = db.Column('Password', postgresql.TEXT, unique = False, nullable=False)
def __repr__(self):
return '<User %r>' % self.user_id
#property
def password_raw(self):
raise AttributeError('password not readable')
#password_raw.setter
def password_raw(self, password):
self.password = bcrypt.generate_password_hash(password)
def verify_password(self, password):
return bcrypt.check_password_hash(self.password, password)
And here is what my database helper function:
def database_helper(self, args):
id = uuid4()
userAuth = AuthenticationModel(user_id=id, email=args['email'], phone_no=args['phone_no'], password_raw=args['password'])
userInfo = PersonalInfoModel(user_id=id, first_name=args['first_name'], last_name=args['last_name'], dob=args['dob'])
db.session.add(userAuth)
db.session.add(userInfo)
db.session.commit()
I found many examples of autogenerating UUID fields. However, if I want to create some user across multiple databases, how can I do that? Because I think I will need to be able to pass the user id to the other tables even if I autogenerate it for one of the tables. Or should I simply use a text field?
uuid4() is generating a UUID instance.
You want to pass a string for the user_id field. Convert it to a string
user_id = str(uuid4())
If you like to pass UUID instance around and get them back in the result, you should update the user_id field in your schema to:
user_id = db.Column('UserId', postgresql.UUID(as_uuid=True), primary_key=True)

insert to Db2 using Flask and sqlalchemy

I have a Flask application with sqlalchemy in it. Running manage db init, manage db migrate and manage db upgrade worked perfectly. So the tables are created on db2 without any problems. When i try to do an insert, i get all kinds of errors.
Traceback (most recent call last):
File "*path*/lib/python3.6/site-packages/ibm_db_dbi.py", line 1258, in _execute_helper
return_value = ibm_db.execute(self.stmt_handler, parameters)
Exception: Binding Error:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
context)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/ibm_db_sa/ibm_db.py", line 107, in do_execute
cursor.execute(statement, parameters)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/ibm_db_dbi.py", line 1356, in execute
self._execute_helper(parameters)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/ibm_db_dbi.py", line 1268, in _execute_helper
raise self.messages[len(self.messages) - 1]
ibm_db_dbi.DatabaseError: ibm_db_dbi::DatabaseError: Binding Error:
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/flask_restplus/api.py", line 319, in wrapper
resp = resource(*args, **kwargs)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/flask_restplus/resource.py", line 44, in dispatch_request
resp = meth(*args, **kwargs)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api/app/api.py", line 37, in get
snp.sensor_info()
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api/app/network.py", line 123, in sensor_info
db_session.commit()
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 153, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 943, in commit
self.transaction.commit()
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 467, in commit
self._prepare_impl()
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 447, in _prepare_impl
self.session.flush()
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2254, in flush
self._flush(objects)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2380, in _flush
transaction.rollback(_capture_exception=True)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise
raise value
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2344, in _flush
flush_context.execute()
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 391, in execute
rec.execute(self)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 556, in execute
uow
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 181, in save_obj
mapper, table, insert)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 830, in _emit_insert_statements
execute(statement, multiparams)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 948, in execute
return meth(self, multiparams, params)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 269, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1060, in _execute_clauseelement
compiled_sql, distilled_params
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1200, in _execute_context
context)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
exc_info
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
raise value.with_traceback(tb)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
context)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/ibm_db_sa/ibm_db.py", line 107, in do_execute
cursor.execute(statement, parameters)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/ibm_db_dbi.py", line 1356, in execute
self._execute_helper(parameters)
File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/ibm_db_dbi.py", line 1268, in _execute_helper
raise self.messages[len(self.messages) - 1]
sqlalchemy.exc.DatabaseError: (ibm_db_dbi.DatabaseError) ibm_db_dbi::DatabaseError: Binding Error: [SQL: 'INSERT INTO "schema"."table" (sensor_id, code, lat, lon, x, y) VALUES (?, ?, ?, ?, ?, ?)'] [parameters: (2855, 30, 59.291658, 18.077241, 411.59730406100783, 769.5522643844229)] (Background on this error at: http://sqlalche.me/e/4xp6)
The model looks like:
class Sensor(Base):
__tablename__ = 'table'
__table_args__ = {'schema':'schema'}
sensor_id = db.Column(db.Integer, primary_key=True)
code = db.Column(db.Integer, unique=True, nullable=False)
lat = db.Column(db.Float, unique=True, nullable=False)
lon = db.Column(db.Float, unique=True, nullable=False)
x = db.Column(db.Float, unique=True, nullable=False)
y = db.Column(db.Float, unique=True, nullable=False)
def __repr__(self):
return '<Sensor %r, x=%r, y=%r>' % self.code % self.x % self.y
The insert code looks like:
from .models import Sensor
from .database import db_session
ins = Sensor(sensor_id = sensor_coords['sensor_code'][0],
code = sensor_coords['sensor'][0],
lat = sensor_coords['lat'][0],
lon = sensor_coords['long'][0],
x = sensor_coords['x'][0],
y = sensor_coords['y'][0])
db_session.add(ins)
db_session.commit()
Looking at the error message, this is how it's trying to bind the sql statement:
[SQL: 'INSERT INTO "schema"."table" (sensor_id, code, lat, lon, x, y) VALUES (?, ?, ?, ?, ?, ?)'] [parameters: (2855, 30, 59.291658, 18.077241, 411.59730406100783, 769.5522643844229)]
which looks correct? (schema and table names are replaced).
My database.py looks like:
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import ibm_db_sa
from app.api import app
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'], convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
Base.metadata.create_all(bind=engine)
I can't figure out whats wrong.. The insert format looks correct?
Appreciate all the help I can get. Thanks!
Got it to work!
Had to cast the types in the insert code as:
ins = Sensor(sensor_id = int(sensor_coords['sensor_code'][0]),
code = int(sensor_coords['sensor'][0]),
lat = float(sensor_coords['lat'][0]),
lon = float(sensor_coords['long'][0]),
x = float(sensor_coords['x'][0]),
y = float(sensor_coords['y'][0]))
Didn't realize numpy used its own int and float types...

sqlalchemy integrity error when using many to many relationship

I'm writting application to store leaked DBs and hashes in MySQL. I want to save some space, so I figured out it may be a good idea to store only unique hashes in Hashes table and put a reference from DBList table. In other words, if I want to get all hashes that belongs to db_id=1 I should ask for them using association table.
For testing purposes I've created these files:
create_fresh_db.py
mydb.py
regex_test.txt
My problem is that association table called dbtohash is filled properly only when I'm inserting unique hashes. If I'm trying to create different name for DB and using the same hashlist (regex_test.txt) an IntegrityError exception is raised. How can I fix that?
EDIT: Following suggestion given in comments. Here are relevant parts of my code. This is how I create many-to-many relationship and association table:
dbtohash = db.Table(
'dbtohash',
db.Column(
'hash_id',
db.Integer,
db.ForeignKey('Hashes.hash_id')),
db.Column(
'db_id',
db.Integer,
db.ForeignKey('DBList.db_id')))
class DBList(db.Model):
__tablename__ = 'DBList'
db_id = db.Column(db.Integer, primary_key=True)
db_name = db.Column(db.String(128), index=True, unique=True)
class Hashes(db.Model):
__tablename__ = 'Hashes'
hash_id = db.Column(db.Integer, primary_key=True)
hash_val = db.Column(db.String(1024), unique=True)
hash_salt = db.Column(db.String(256))
hash_plain = db.Column(db.String(256))
subscriptions = db.relationship(
'DBList', secondary=dbtohash, backref=db.backref(
'dbtohashref', lazy='dynamic'))
And this is how I insert data to it:
new list
# if we have some matched hashes...
if len(hash_pass) > 0:
# create new list
newlist = DBList(
db_name = '{}_{}'.format('db', datetime.utcnow()),
)
print('newlist: {}'.format(newlist))
# try to insert DB name to DBList table, raise exception if name is taken
try:
db.session.add(newlist)
db.session.commit()
db.session.flush()
except IntegrityError:
db.session.rollback()
print('this DB name is taken, choose another one', 'danger')
insert hashes to DB
failed=[]
for hash in hash_pass:
inserthash = Hashes (
hash_val = hash,
)
try:
db.session.add(inserthash)
db.session.commit()
db.session.flush()
hashid=inserthash.hash_id
except IntegrityError:
db.session.rollback()
failed.append(hash)
hashid=db.session.query(Hashes.hash_id).filter_by(hash_val=hash).one()[0]
# fill up association table
newlist.dbtohashref.append(inserthash)
db.session.commit()
if failed:
print('failed: {}'.format(failed))
problematic line is: newlist.dbtohashref.append(inserthash)
EDIT 2: full trace attached
Traceback (most recent call last):
File "mydb.py", line 88, in <module>
db.session.commit()
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 153, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 943, in commit
self.transaction.commit()
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 467, in commit
self._prepare_impl()
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 447, in _prepare_impl
self.session.flush()
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2254, in flush
self._flush(objects)
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2380, in _flush
transaction.rollback(_capture_exception=True)
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2344, in _flush
flush_context.execute()
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 391, in execute
rec.execute(self)
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 556, in execute
uow
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 181, in save_obj
mapper, table, insert)
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 866, in _emit_insert_statements
execute(statement, params)
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 948, in execute
return meth(self, multiparams, params)
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 269, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1060, in _execute_clauseelement
compiled_sql, distilled_params
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1200, in _execute_context
context)
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
exc_info
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
context)
File "/home/user/envs/project/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 507, in do_execute
cursor.execute(statement, parameters)
File "/home/user/envs/project/local/lib/python2.7/site-packages/pymysql/cursors.py", line 165, in execute
result = self._query(query)
File "/home/user/envs/project/local/lib/python2.7/site-packages/pymysql/cursors.py", line 321, in _query
conn.query(q)
File "/home/user/envs/project/local/lib/python2.7/site-packages/pymysql/connections.py", line 860, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/home/user/envs/project/local/lib/python2.7/site-packages/pymysql/connections.py", line 1061, in _read_query_result
result.read()
File "/home/user/envs/project/local/lib/python2.7/site-packages/pymysql/connections.py", line 1349, in read
first_packet = self.connection._read_packet()
File "/home/user/envs/project/local/lib/python2.7/site-packages/pymysql/connections.py", line 1018, in _read_packet
packet.check_error()
File "/home/user/envs/project/local/lib/python2.7/site-packages/pymysql/connections.py", line 384, in check_error
err.raise_mysql_exception(self._data)
File "/home/user/envs/project/local/lib/python2.7/site-packages/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
sqlalchemy.exc.IntegrityError: (pymysql.err.IntegrityError) (1062, u"Duplicate entry 'd38b70e33d7ae06c1696910eb6f93ca4' for key 'hash_val'") [SQL: u'INSERT INTO `Hashes` (hash_val, hash_salt, hash_plain) VALUES (%(hash_val)s, %(hash_salt)s, %(hash_plain)s)'] [parameters: {'hash_salt': None, 'hash_val': 'd38b70e33d7ae06c1696910eb6f93ca4', 'hash_plain': None}] (Background on this error at: http://sqlalche.me/e/gkpj)

Categories