Re: [web2py] Re: Referencing another table

2016-10-15 Thread Val K
If you want to have strict DB-constraints, you can: 

db.define_table('barcode',
Field('code', type='string', unique=True, notnull=True), #  
*consider 
just 'code', not 'barcode'* - it's necessary to prevent possible 
hard-to-catch bugs during SQLFORMs processing   
format='%(code)s'
)

db.define_table('bursary_users',
 ...
Field('barcode', type='reference barcode', notnull=True, 
unique=True),
 ...)

db.define_table('bursary_entries',
db.bursary_users.barcode.clone(), # - .clone() is 
undocumented, but why?   
Field('entry_date', type='datetime', default=request.now, 
readable=False, writable=False),
format='%(barcode)s'
)

#be careful - *above code could break your existing db-tables  *

# this solution require additional logic to process insert in bursary_
users,  look for   One form for multiple tables   

def new_user():

   #this would be invoked before insert in  db.bursary_users
   def reg_bar_code(form):
   new_code = form.vars.pop('code', None) #get user input for 
 db.barcode.code
   #any additional logic goes here
   form.vars.barcode =  db.barcode.insert(code = new_code)  # returns 
id of  new_code
# so, form.vars.barcode would be used to  insert in  db.bursary_
users.barcode

   db.bursary_users.barcode.writable = False  
 db.barcode.code.label  = 'barcode'  # 
   form=SQFORM.factory(db.barcode, db.bursary_users) 
   if form.process(onvalidation = reg_bar_code).accepted:
 db.bursary_users.insert(**db.bursary_users._filter_fields(form.
vars))
   







On Saturday, October 15, 2016 at 5:08:13 PM UTC+3, James Booth wrote:
>
> Ah okay. It should only be done via. A validated form anyway but I see the 
> potential risk. Is the other option to set the barcode as the PK in my 
> bursary_users table and 'reference' it?
>

-- 
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 the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [web2py] Re: Referencing another table

2016-10-15 Thread 黄祥
i think it's can be done by record representation format, either in table 
format or in requires validation format representation. (the 
table bursary_entries just save the id for that refered to bursary_users, 
but it will shown the barcode, if you set the format correctly either in 
table format or requires validation)
ref:
http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#format--Record-representation

best regards,
stifan
>
>

-- 
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 the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [web2py] Re: Referencing another table

2016-10-15 Thread vValentine1337
Ah okay. It should only be done via. A validated form anyway but I see the
potential risk. Is the other option to set the barcode as the PK in my
bursary_users table and 'reference' it?

-- 
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 the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.