Misnamed datetime attributes in mysql/base.py
Using SQLAlchemy with a DateTime column on MySQL gives me the following stack trace:
Traceback (most recent call last):
File "/Users/tla/Projects/PBW/main.py", line 9, in <module>
our_people = session.query(data.Person).filter_by(name="Alexios").all()
File "/Users/tla/anaconda/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2279, in all
return list(self)
File "/Users/tla/anaconda/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 72, in instances
rows = [process[0](row, None) for row in fetch]
File "/Users/tla/anaconda/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 451, in _instance
populate_state(state, dict_, row, isnew, only_load_props)
File "/Users/tla/anaconda/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 305, in populate_state
populator(state, dict_, row)
File "/Users/tla/anaconda/lib/python2.7/site-packages/sqlalchemy/orm/strategies.py", line 154, in fetch_col
dict_[key] = row[col]
File "/Users/tla/anaconda/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 769, in process
microseconds = value.microseconds
AttributeError: 'datetime.datetime' object has no attribute 'microseconds'
...which makes sense, as indeed the attribute is called 'microsecond' and, on the following line, it should be 'second' rather than 'seconds'. Here is the very simple patch:
--- base.py 2014-06-25 11:03:02.000000000 +0200
+++ /Users/tla/anaconda/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/base.py 2014-06-25 11:03:08.000000000 +0200
@@ -766,8 +766,8 @@
def process(value):
# convert from a timedelta value
if value is not None:
- microseconds = value.microseconds
- seconds = value.seconds
+ microseconds = value.microsecond
+ seconds = value.second
minutes = seconds // 60
return time(minutes // 60,
minutes % 60,
Comments (12)
-
repo owner -
repo owner oh, unless you've applied the TIME type to a MySQL column that is actually a DATETIME or TIMESTAMP. that would be on your end.
-
reporter Aha, yes, to a TIMESTAMP. How absent-minded of me! Apologies for the trouble.
-
reporter - changed status to invalid
Mistaken bug report
-
repo owner - changed milestone to 0.9.8
-
repo owner - changed milestone to 0.9.7
-
Hi,
I am getting the same error when trying to use
aldjemy
ordjango-sabridge
packages and querying usingSession
. (I am trying to integrate django with sqla)How about a patch that will check if the instance is of datetime.time and if so, will just return the datetime.time object ?
I am using MySQL-python==1.2.5
-
repo owner @tackler why are you using the wrong datatype for the column as described above?
-
No, actually django returns
datetime.time
and notdatetime.timedelta
forTimeField
(which MySQL stores astime
). It overrides MySQLdb'sconversions
(https://github.com/django/django/blob/master/django/db/backends/mysql/base.py#L44). These packages give this value to SQLA.So, I am not sure if it's okay to ask for a change in SQLA itself or if there's some other way I can make this work.
Sorry for the confusion.
-
repo owner so....why not propose a patch to django-sabridge? since that's the program that's supposed to do the correct conversions between the two packages.
-
repo owner on this end, the best I could do would be to catch AttributeError, and then do the check and just raise an error that's descriptive. because first off we try very hard not to put isinstance() into a data conversion function and secondly because the datatype should be used correctly in the first place.
-
Got it. Thanks for the help! I will go bug them.
- Log in to comment
this is highly unusual because that code is handling a datetime.timedelta() object, not a datetime.time(). I've just added this test case:
and ran it against: mysql-python, oursql, mysqlconnector, pymysql. All return a timedelta(), and if we use the patch above, it breaks:
so, this is not making much sense how you're getting a datetime in there. What database, Python version, DBAPI in use? Special datatypes in play here?