Retrieving record MySql Database PyDAL

565 Views Asked by At

I have a MySql Databse where my Web2Py application store data. My app can with success insert records. The problem, right now, is while retrieving information. Below follows my DAL connection and the author table definition.

db = DAL('mysql://myUser:myPassWord@localhost/myDB',
      migrate_enabled=true, check_reserved=['all'],fake_migrate_all=True, 
      migrate=False)

db.define_table('author',
            Field('author_id', 'string'),
            Field('name', 'string', requires=[IS_NOT_EMPTY()]),
            ...                            
            Field('lang', 'string', requires=[IS_NOT_EMPTY()]),
            primarykey=['author_id'])

The problem is when I try retrieve all the information of author table (which means in SQL select * from author) in my controller with:

crud.search(db.author)

I got an error:

<class 'AttributeError'> 'Table' object has no attribute 'id'
...
query   undefined
args    {}
args.get    <built-in method get of dict object>
table   <Table author (author_id, name, screen_n...l, 
url, verified, lang)>
table.id    undefined

In my table definition as you can see my primary key is the field author_id that is varchar (string python), and I specify as primary key in my table DAL definition, as said in the Web2Py DAL Documentation (primaryKey). But when I make a select in that table I got the error above. What I need to do? I already search that but I didnt found no successful solution. Somenone can help me out? Thanks.

1

There are 1 best solutions below

0
On

web2py does not fully support keyed tables (i.e., those whose primary key is not an auto-incrementing ID field). In particular, crud.search does not support such tables.

If it all possible, it is best to include a field of type id in your tables to serve as the primary key (if you do not specify such a field, the DAL automatically includes one named "id").

Also, note that Crud is deprecated in favor of SQLFORM.grid.