SyntaxError: table already defined: definition

56 views
Skip to first unread message

Maurice Waka

unread,
Apr 22, 2018, 11:08:05 AM4/22/18
to web2py-users
I have a module that imports another that reads a db as follows:

from gluon import current
from pydal import DAL, Field
from unidecode import unidecode
from applications.Hestque_Wellness.modules.Hestquewell.q_refs import definition
db
= DAL('sqlite://storage.sqlite')
current
.db = db
define
= definition()


The imported module is as follows:

from gluon import current
from pydal import DAL, Field
db
= DAL('sqlite://storage.sqlite')
current
.db = db


def reference():
    db
= current.db
    db
.define_table('definition', Field('definition'), migrate=False)
    rows
= db(db.definition).select()
   
for row in rows:
       
return row.definition

I keep getting this error:

    definition = definition()
File "applications/Hestque_Wellness/modules/Hestquewell/q_refs.py", line 321, in definition
db.define_table('definition', Field('definition'), migrate=False)
File "/usr/local/lib/python2.7/dist-packages/pydal/base.py", line 571, in define_table
raise SyntaxError('table already defined: %s' % tablename)
SyntaxError: table already defined: definition
what could be the problem?

Kind regards

Anthony

unread,
Apr 22, 2018, 11:31:07 AM4/22/18
to web2py-users
Hard to say for sure without seeing the code for the definition() function, but presumably the reference() function is being called more than once -- every time after the first, it is attempting to redefine a table that has already been defined (which is not allowed unless you set redefine=True).

Also, there is no reason to keep recreating the DAL object (and if you are going to do that, there is no reason to keep adding it to current, as you are not using the object added to current but simply creating a new object in each module). Just create the DAL object once in a model file and add it to current there.

Actually, a better approach may be to just make the db object an argument of the functions that use it, which will make everything more explicit:

define = definition(db)

Anthony

Maurice Waka

unread,
Apr 22, 2018, 12:20:56 PM4/22/18
to web...@googlegroups.com
I tried not recreating the DAL object but got the error : DAL has no attribute: 'definition' 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/7xCGTfc6xAA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
Apr 22, 2018, 1:54:59 PM4/22/18
to web2py-users
Can't help without the code.

Maurice Waka

unread,
Apr 22, 2018, 2:00:49 PM4/22/18
to web...@googlegroups.com
Will send it once I get back to the comp 

On Sun, 22 Apr 2018, 20:55 Anthony <abas...@gmail.com> wrote:
Can't help without the code.

Maurice Waka

unread,
Apr 22, 2018, 3:48:14 PM4/22/18
to web...@googlegroups.com
I looked at my question, the reference function was actually a typo, my apology, it was supposed to be definition().
I worked it out again as follows:
model:

db = DAL('sqlite://storage.sqlite', migrate=False, fake_migrate=False)
response.generic_patterns = ['*'] if request.is_local else []
auth = Auth(db, hmac_key=Auth.get_or_create_key())
auth.define_tables()
crud, service, plugins = Crud(db), Service(), PluginManager()
current.db= db
db.define_table('definition',
    Field('definition', 'text', length= 1000000, default="We'll update soon.", notnull=True))



module:#define code

from gluon import current
from pydal import DAL, Field
db = current.db

def definition():
    rows = db(db.definition).select()
    for row in rows:
        return row.definition

With this , the error disappeared but when running the code in web2py, i now have a new headache:

  File "applications/Hestque_Wellness/modules/Hestquewell/abnvalinterpretation/wellabnvalinterpret0.py", line 101, in wellabnvalinterpret
rows = db(db.health).select()
File "/usr/local/lib/python2.7/dist-packages/pydal/objects.py", line 2250, in select
return adapter.select(self.query, fields, attributes)
File "/usr/local/lib/python2.7/dist-packages/pydal/adapters/sqlite.py", line 82, in select
return super(SQLite, self).select(query, fields, attributes)
File "/usr/local/lib/python2.7/dist-packages/pydal/adapters/base.py", line 762, in select
return self._select_aux(sql, fields, attributes, colnames)
File "/usr/local/lib/python2.7/dist-packages/pydal/adapters/base.py", line 718, in _select_aux
rows = self._select_aux_execute(sql)
File "/usr/local/lib/python2.7/dist-packages/pydal/adapters/base.py", line 712, in _select_aux_execute
self.execute(sql)
File "/usr/local/lib/python2.7/dist-packages/pydal/adapters/__init__.py", line 63, in wrap
if not args[0].connection:
File "/usr/local/lib/python2.7/dist-packages/pydal/connection.py", line 36, in connection
return getattr(THREAD_LOCAL, self._connection_uname_)
AttributeError: 'thread._local' object has no attribute '_pydal_connection_140327873342608_17719'

The module wellabnvalinterpret0 imports definition()function. It accesses the health db.

Kind regards

On Sun, Apr 22, 2018 at 9:00 PM, Maurice Waka <mauri...@gmail.com> wrote:
Will send it once I get back to the comp 
On Sun, 22 Apr 2018, 20:55 Anthony <abas...@gmail.com> wrote:
Can't help without the code.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/7xCGTfc6xAA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

Maurice Waka

unread,
Apr 22, 2018, 7:23:45 PM4/22/18
to web...@googlegroups.com
I solved the thread.local error by declaring the rows=db(db.health).select() as a global object in my code. 
I hope I won't get into trouble. 
Thanks @ Anthony. 

To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Anthony

unread,
Apr 23, 2018, 10:24:30 AM4/23/18
to web2py-users
On Sunday, April 22, 2018 at 3:48:14 PM UTC-4, Maurice Waka wrote:
module:#define code

from gluon import current
from pydal import DAL, Field
db = current.db

As noted here, you should not assign values from current at the top level of a module. Instead, move the db = current.db line into the definition() function. Actually, it is simpler and more explicit to just make db an argument of the definition() function, so you can pass it in directly rather than bothering attaching it to current.
 
def definition():
    rows = db(db.definition).select()
    for row in rows:
        return row.definition

What is the point of selecting all the records and running the for loop -- the function will simply end up returning the first record only?

Anthony

Anthony

unread,
Apr 23, 2018, 10:26:23 AM4/23/18
to web2py-users
On Sunday, April 22, 2018 at 7:23:45 PM UTC-4, Maurice Waka wrote:
I solved the thread.local error by declaring the rows=db(db.health).select() as a global object in my code.

That's OK in a model (but not at the top level of a module) if you really need it on every request, but wasteful otherwise.

Anthony
Reply all
Reply to author
Forward
0 new messages