I'm developing a db project using web2py framework where in I need to write a query to find the total bill amount that is obtained by multiplying the quantity and unit_price.
Tables:
db.define_table('item',
Field('name', notnull = True,unique=True),
Field('price'),
format = '%(name)s')
db.define_table('customer',
Field('item_name', 'reference item'),
Field('item_price','reference item' ),
Field('name', notnull = True),
Field('email',requires = IS_EMAIL(error_message='invalid email!'),unique='True'),
Field('adress',requires=IS_NOT_EMPTY()),
Field('city'),
Field('quantity'),
Field('state',default='KARNATAKA'),
Field('contact_number'),
Field('Total_price',
compute=lambda r: r['quantity'] * r['item_price']),
format='%(name)s')
db.item.name.requires = IS_NOT_IN_DB(db, db.item.name)
db.customer.item_name.requires = IS_IN_DB(db, db.item.id, '%(name)s')
db.item.price.requires = IS_NOT_IN_DB(db, db.item.price)
db.customer.item_price.requires = IS_IN_DB(db, db.item.id, '%(price)s')`
Output:
customer.id customer.item_name customer.item_price customer.name customer.email customer.adress customer.city customer.quantity
customer.state customer.contact_number customer.Total_price:
Gold Winner Palm Reshma reshmagunda@g... Near Bus stand Mudalgi 2 KARNATAKA 7423089630 2222
If I use this query for single table calculation it works fine, but for foreign key linked tables I'm getting junk values...
Price field is in item; for above query it is 65/-
it is foreign key for customer table and quantity is 2
. Expected output is 130
but generated query output is 2222
.
What modifications do I need to make to get the expected output?