The problem is that this:

print db(images_db.image).select()

should be

print images_db(images_db.image).select()

because you cannot have a query involving a table sent to database that 
does not have the table.

You can also check with

>>> 'images_db' in db.tables()
False

>>> 'images_db' in images_db.tables()
True



On Saturday, 21 April 2012 16:59:41 UTC-5, Cody Goodman wrote:
>
> For my project, I need to connect to multiple databases and get 
> information from them. I didn't think this would be a problem with web2py, 
> but it was. I thought maybe I need to rebuild the db from scratch, but 
> still had problems. Finally, I went through the introductory 'images' 
> tutorial and changed it to use an alternate mysql database. I still got the 
> same errors, below is the code:
>
> *db.py*
> db = DAL("mysql://root:@localhost/web2py")
> images_db = DAL("mysql://root:@localhost/images_test")
>
>
> images_db.define_table('image',
>    Field('title', unique=True),
>    Field('file', 'upload'),
>    format = '%(title)s')
>
>
> images_db.define_table('comment',
>    Field('image_id', images_db.image),
>    Field('author'),
>    Field('email'),
>    Field('body', 'text'))
>
>
>
>
> Then I went to the admin page for 'images' and clicked the 'shell' link 
> under 'controllers' and did the following: (after I went to the index page 
> to generate the 'images':
>
> In [1] : print db(images_db.image).select()
> Traceback (most recent call last):
>   File "/home/cody/Downloads/web2py/gluon/contrib/shell.py", line 233, inrun
>     exec compiled in statement_module.__dict__
>   File "<string>", line 1, in <module>
>   File "/home/cody/Downloads/web2py/gluon/dal.py", line 7577, in select
>     fields = adapter.expand_all(fields, adapter.tables(self.query))
>   File "/home/cody/Downloads/web2py/gluon/dal.py", line 1172, inexpand_all
>     for field in self.db[table]:
>   File "/home/cody/Downloads/web2py/gluon/dal.py", line 6337, in__getitem__
>     return dict.__getitem__(self, str(key))
> KeyError: 'image'
>
>
> In [2] : print images_db.has_key('image')
> True
>
>
> In [3] : print images_db
> <DAL {'_migrate_enabled': True, '_lastsql': "SET 
> sql_mode='NO_BACKSLASH_ESCAPES';", '_db_codec': 'UTF-8', '_timings': [('SET 
> FOREIGN_KEY_CHECKS=1;', 0.00017380714416503906), ("SET 
> sql_mode='NO_BACKSLASH_ESCAPES';", 0.00016808509826660156)], 
> '_fake_migrate': False, '_dbname': 'mysql', '_request_tenant': 
> 'request_tenant', '_adapter': <gluon.dal.MySQLAdapter object at 0x2b84750
> >, '_tables': ['image', 'comment'], '_pending_references': {}, 
> '_fake_migrate_all': False, 'check_reserved': None, '_uri': 
> 'mysql://root:@localhost/images_test', 'comment': <Table {'body': <gluon.
> dal.Field object at 0x2b844d0>, 'ALL': <gluon.dal.SQLALL object at 
> 0x2b84090>, '_fields': ['id', 'image_id', 'author', 'email', 'body'], 
> '_sequence_name': 'comment_sequence', '_plural': 'Comments', 'author': <
> gluon.dal.Field object at 0x2b84e10>, '_referenced_by': [], '_format': 
> None, '_db': <DAL {...}>, '_dbt': 
> 'applications/images/databases/e1e448013737cddc822e303fe20f8bec_comment.table'
> , 'email': <gluon.dal.Field object at 0x2b84490>, '_trigger_name': 
> 'comment_sequence', 'image_id': <gluon.dal.Field object at 0x2b84050>, 
> '_actual': True, '_singular': 'Comment', '_tablename': 'comment', 
> '_common_filter': None, 'virtualfields': [], '_id': <gluon.dal.Field 
> object at 0x2b84110>, 'id': <gluon.dal.Field object at 0x2b84110>, 
> '_loggername': 'applications/images/databases/sql.log'}>, 'image': <Table 
> {'ALL': <gluon.dal.SQLALL object at 0x2b84850>, '_fields': ['id', 'title', 
> 'file'], '_sequence_name': 'image_sequence', 'file': <gluon.dal.Field 
> object at 0x2b847d0>, '_plural': 'Images', 'title': <gluon.dal.Field 
> object at 0x2b84610>, '_referenced_by': [('comment', 'image_id')], 
> '_format': '%(title)s', '_db': <DAL {...}>, '_dbt': 
> 'applications/images/databases/e1e448013737cddc822e303fe20f8bec_image.table'
> , '_trigger_name': 'image_sequence', '_loggername': 
> 'applications/images/databases/sql.log', '_actual': True, '_tablename': 
> 'image', '_common_filter': None, 'virtualfields': [], '_id': <gluon.dal.
> Field object at 0x2b848d0>, 'id': <gluon.dal.Field object at 0x2b848d0>, 
> '_singular': 'Image'}>, '_referee_name': '%(table)s', '_migrate': True, 
> '_pool_size': 0, '_common_fields': [], '_uri_hash': 
> 'e1e448013737cddc822e303fe20f8bec'}>
>
>
> Now I don't quite understand why I am getting errors here, everything 
> appears to be in order. I thought web2py supported multiple databases? Am I 
> doing it wrong? The appadmin works fine, perhaps I'll edit it and get it to 
> raise an error with the code it's generating... any help would be 
> appreciated.
>
> - Cody
>
>
> UPDATE:
>
> I just tried this:
>
> *MODELS/DB.PY*
> db = DAL("mysql://root:@localhost/web2py")
>
> images_db = DAL("mysql://root:@localhost/images_test")
>
>
> images_db.define_table('image',
>    Field('title', unique=True),
>    Field('file', 'upload'),
>    format = '%(title)s')
>
>
> images_db.define_table('comment',
>    Field('image_id', images_db.image),
>    Field('author'),
>    Field('email'),
>    Field('body', 'text'))
>
>
> *CONTROLLERS/DEFAULT.PY*
> def index():
>     """
>     example action using the internationalization operator T and flash
>     rendered by views/default/index.html or views/generic.html
>     """
>     if images_db.has_key('image'):
>         rows = db(images_db.image).select()
>     else:
>         rows = 'nope'
>     #rows = dir(images_db)
>     return dict(rows=rows)
>
>
> *VIEWS/DEFAULT/INDEX.HTML*
> {{left_sidebar_enabled,right_sidebar_enabled=False,True}}
> {{extend 'layout.html'}}
>
>
> these are the rows:
> {{=rows }}
>
> Again, very confused by all of this. Appreciate any help.
>

Reply via email to