Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError when executing queries #701

Closed
niphlod opened this issue Jan 26, 2015 · 5 comments
Closed

ValueError when executing queries #701

niphlod opened this issue Jan 26, 2015 · 5 comments

Comments

@niphlod
Copy link
Member

niphlod commented Jan 26, 2015

From buzypi on March 20, 2014 17:15:23

Here is how my various files look:

models/db.py:

from gluon import current
db = DAL('%s://%s:%s__at__%s/%s' % (...))
current.db = db

modules/modelmanager.py:
class ModelManager:
def init(self):
self.db = current.db

def get_something_from_db(self, **kwargs):
    return self.db(...).select(...)

In the end of this module I have a singleton instance of this class

which I intend to use in a bunch of places

mmanager = ModelManager()

controllers/mycontroller.py
from modelmanager import mmanager
def foo():
db_data = mmanager.get_something_from_db(...)

Every once in a while I see this error:
File "/var/deploy/jnaapti/web2py/gluon/dal.py", line 1956, in log_execute
if not self.connection: raise ValueError(a[0])
ValueError:

My questions are:

  1. Is this way of using models via modules wrong? I read in many places that this is ok.
  2. What could be the cause of this error and how do I fix this? This is extremely frustrating and I have already spent more than 2 days trying to fix this issue. What version of the product are you using? On what operating system? Ubuntu 12.04 32-bit Server
    Linux dev 3.2.0-58-generic-pae #88-Ubuntu SMP Tue Dec 3 18:00:02 UTC 2013 i686 i686 i386 GNU/Linux

Version 2.9.5-stable+timestamp.2014.03.16.02.35.39 Please provide any additional information below. What is the right way of moving common model code to a separate module?

Original issue: http://code.google.com/p/web2py/issues/detail?id=1905

@pi-aej
Copy link

pi-aej commented Feb 18, 2015

I'm also encountering this issue, but it's 100% reproducible for me.

I have a "sessiondata" table and a "videodata" table, videodata contains a FK reference to a sessiondata PK id.

A GET to following URL pattern will always give me a ValueError:

patterns = ['/sessiondata/session_uuid/{sessiondata.session_uuid}/videos[videodata.session_id]'] #All entries from the video table matching session uuid
## e.g. http://127.0.0.1:8000/api/sessiondata/session_uuid/TESTTEST00004000A0003000000000001/videos
  File "...web2py\gluon\dal.py", line 1952, in log_execute
    if not self.connection: raise ValueError(a[0])
ValueError: SELECT  sessiondata.id, sessiondata.session_uuid FROM sessiondata WHERE (sessiondata.id = 4) LIMIT 1 OFFSET 0;

(The result of the above query is actually id=4 session_uuid=TESTTEST00004000A0003000000000001 so the rows being requested do exist and there isn't anything wrong with the SQL.)

It seems that the DAL has somehow lost it's connection (it gets set to None) before completing the join. Other, more simple queries like /sessiondata/session_uuid/{sessiondata.session_uuid}/ evaluate fine.

Web2Py version: 2.9.11-stable+timestamp.2014.09.15.23.35.11
Python version: 2.7.1
OS: Windows 7 x64 & Debian 7 x64
Most importantly (as I suspect this might be related to table locking):

db = DAL('sqlite://storage.sqlite',pool_size=10,check_reserved=['all'])

Like @niphlod , I've spent about 2 days trying to figure out what is happening. This is just a simple FK join, so how can I make it work?

@butsyk
Copy link

butsyk commented Mar 3, 2015

I have absolutely the same issue with 2.9.12-stable+timestamp.2015.01.17.06.11.03
on win7 + mysql.
Before it was fine, now mostly the second select show self.connection = None
File "C:...\gluon\dal\adapters\base.py", line 1304, in log_execute
if not self.connection: raise ValueError(a[0])
ValueError: SELECT category.id, category.name, ....

I create connection like:

db = DAL(config.db_uri)
current.db = db

and use current.db everywhere including modules.

Thanks.

My bad.
Found the issue, I've saved current.db to static class variable.
Updated (not to save)
No issues.

@niphlod
Copy link
Member Author

niphlod commented Mar 3, 2015

IMHO this has been fixed, and is closely related on how people misuse the DAL object in their apps. Until further notices by @pi-aej for 2.9.12, I'd close this.

@niphlod
Copy link
Member Author

niphlod commented Mar 8, 2015

ok, closing in absence of further notices

@niphlod niphlod closed this as completed Mar 8, 2015
@flagist0
Copy link

In my case it was caused by usage of DAL object in current on module level (to avoid writing current.db instead of db all the time):

from gluon import current
db = current.db

def func():
   #module level db object usage here

As far as I understand this is what happened:
module is loaded one time, and after module level variable (db) is instantiated, it won't change anymore
thread in pool is destroyed, but db is still referring to it
code crashes

While accessing db in current inside the function retrieves actual value at each call and everything goes fine. Just for others googling the cause of such issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants