Question

I have a legacy database called my_legacy_db which is separate from the normal db.

my_legacy_db

users - email - username - name

So cliff, your first part would work to generate field names and put everything in a dict to build the query's. The problem is when I do this query:

db().select(my_legacy_db.users)

I get this error:
In [20] : db().select(my_legacy_db.users)
Traceback (most recent call last):
  File "/opt/web-apps/web2py/gluon/contrib/shell.py", line 233, in run
    exec compiled in statement_module.__dict__
  File "<string>", line 1, in <module>
  File "/opt/web-apps/web2py/gluon/dal.py", line 7578, in select
    return adapter.select(self.query,fields,attributes)
  File "/opt/web-apps/web2py/gluon/dal.py", line 1307, in select
    sql = self._select(query, fields, attributes)
  File "/opt/web-apps/web2py/gluon/dal.py", line 1196, in _select
    raise SyntaxError, 'Set: no tables selected'
SyntaxError: Set: no tables selected

In [21] : print (flickr_db.users)
users

In [22] : print flickr_db
<DAL {'_migrate_enabled': True, '_lastsql': "SET sql_mode='NO_BACKSLASH_ESCAPES';", '_db_codec': 'UTF-8', '_timings': [('SET FOREIGN_KEY_CHECKS=1;', 0.0002460479736328125), ("SET sql_mode='NO_BACKSLASH_ESCAPES';", 0.00025606155395507812)], '_fake_migrate': False, '_dbname': 'mysql', '_request_tenant': 'request_tenant', '_adapter': <gluon.dal.MySQLAdapter object at 0x91375ac>, '_tables': ['users'], '_pending_references': {}, '_fake_migrate_all': False, 'check_reserved': None, '_uri': 'mysql://CENSORED', 'users': <Table 'username': <gluon.dal.Field object at 0x9137b6c>, '_db': <DAL {...}>, 'cycled': <gluon.dal.Field object at 0x94d0b8c>, 'id': <gluon.dal.Field object at 0x95054ac>, 'ALL': <gluon.dal.SQLALL object at 0x969a7ac>, '_sequence_name': 'users_sequence', 'name': <gluon.dal.Field object at 0x9137ecc>, '_referenced_by': [], '_singular': 'Users', '_common_filter': None, '_id': <gluon.dal.Field object at 0x95054ac>}>, '_referee_name': '%(table)s', '_migrate': True, '_pool_size': 0, '_common_fields': [], '_uri_hash': 'dfb3272fc537e3339819a1549180722e'}>

Am I doing something wrong here? Is the legacy db not built in /databases right? Thanks in advance for any help.

UPDATE: I tried as anthony suggested in the model shell:

In [3] : db(my_legacy_db.users).select()
Traceback (most recent call last):
  File "/opt/web-apps/web2py/gluon/contrib/shell.py", line 233, in run
    exec compiled in statement_module.__dict__
  File "<string>", line 1, in <module>
  File "/opt/web-apps/web2py/gluon/dal.py", line 7577, in select
    fields = adapter.expand_all(fields, adapter.tables(self.query))
  File "/opt/web-apps/web2py/gluon/dal.py", line 1172, in expand_all
    for field in self.db[table]:
  File "/opt/web-apps/web2py/gluon/dal.py", line 6337, in __getitem__
    return dict.__getitem__(self, str(key))
KeyError: 'users'

Now I know that users is defined in my_legacy_db, and all syntax is correct. Is this an error that is there because the db files aren't generating correctly? Or am I still doing something wrong with the select syntax?

Was it helpful?

Solution

If "users" is the name of a table and you want to select all records and all fields, you would do:

db(my_legacy_db.users).select()

The query goes inside db(), not inside select() (select() is where you list the fields you want returned, or leave it empty if you want all fields). Note, in the above line, my_legacy_db.users is not actually a query but just a table -- that's a shortcut to tell web2py you want all records in the table.

You could also do:

db().select(my_legacy_db.users.ALL)

That indicates you want all fields, and by excluding the query, it assumes you want all records in the table.

See the book for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top