web2py / pydal

A pure Python Database Abstraction Layer
BSD 3-Clause "New" or "Revised" License
491 stars 137 forks source link

report error when use pydal 17.3 in flask debug mode #476

Open wanghaiqing2015 opened 7 years ago

wanghaiqing2015 commented 7 years ago

shows: AttributeError: 'thread._local' object has no attribute '_pydal_connection_65073872_18848'

but use pydal 16.3 or not in debug mode, it can works.

========================================================

File "C:\Python27\lib\site-packages\pydal\objects.py", line 2210, in select return adapter.select(self.query, fields, attributes) File "C:\Python27\lib\site-packages\pydal\adapters\base.py", line 760, in select return self._select_aux(sql, fields, attributes, colnames) File "C:\Python27\lib\site-packages\pydal\adapters\base.py", line 716, in _select_aux rows = self._select_aux_execute(sql) File "C:\Python27\lib\site-packages\pydal\adapters\base.py", line 710, in _select_aux_execute self.execute(sql) File "C:\Python27\lib\site-packages\pydal\adapters__init__.py", line 63, in wrap if not args[0].connection: File "C:\Python27\lib\site-packages\pydal\connection.py", line 36, in connection return getattr(THREAD_LOCAL, self._connectionuname) AttributeError: 'thread._local' object has no attribute '_pydal_connection_64199056_28088' 'thread._local' object has no attribute '_pydal_connection_64199056_28088'

wanghaiqing2015 commented 7 years ago

set use_reloader to False, it can works.

app.run(host='0.0.0.0',port=2133,use_reloader=False)

gi0baro commented 7 years ago

@wanghaiqing2015 this is because you have to manually open up and close the connection in flask.

wanghaiqing2015 commented 7 years ago

@gi0baro what your mean?

Is my code any problem?

wanghaiqing2015 commented 7 years ago

@gi0baro How to solve it?

gi0baro commented 7 years ago

@wanghaiqing2015 you have to write a middleware that should be executed on every route. When request arrives you should call db._adapter.reconnect() and when you completed the response you should call db._adapter.close().

wanghaiqing2015 commented 7 years ago

ok

gi0baro commented 7 years ago

@wanghaiqing2015 for more details you can check the weppy database pipe and try to convert it to the flask middlewares: https://github.com/gi0baro/weppy/blob/release/weppy/orm/base.py#L30#L44

ichux commented 6 years ago

@gi0baro the correct link is https://github.com/gi0baro/weppy/blob/release/weppy/orm/base.py#L31#L45

gi0baro commented 6 years ago

@ichux actually those lines depends on weppy implementation of pydal. The best reference is https://github.com/gi0baro/weppy/blob/v1.1.2/weppy/orm/base.py#L29#L43 Those lines are valid with vanilla pydal