If there's already a link between the user and the company, you want the 
company to NOT appear in the drop down, right?

In order for this to work you have to have the user id. Ordinarily it would 
be the last item on request.args

So, simplifying because I don't enjoy typing all that much, the model 
becomes:

db.define_table('user_company',
  Field(user_id, 'reference auth_user'),
  Field(company_id, 'reference company')
  )

Get rid of that unikey field. 

Then in the controller

skip_these_records_query = db.user_company.user_id != request.args(-1)
db.user_company.company_id.requires=IS_IN_DB(
  db(skip_these_records_query), 'company.id', '%(company_name)s', 
zero='Pick a company'
)

#do the check on the db level, just because ...
def dupe_check(form):
if db((db.company_user.user_id==request.args(-1)) &
       (db.company_user.company_id==form.vars.company_id)
        ).count():
    form.errors['company_id'] = 'Company already associated with this user.'

form = SQLFORM(blah ....
if form.process(dupe_check).accepted:
    do_stuff ....

And while you're at it, get rid of those ondelete attributes, unless you 
want your table littered with orphan records. A reference record is just 
taking up space if the referee is no longer in the db.

All of this stuff is in the on-line manual, by the way. 

A good motto is "Type less, read more."
Also "Exercise the brain, not the fingers."

On Monday, December 16, 2013 12:56:37 PM UTC-5, Michel Hayek wrote:
>
> Guys,
>
> i changed the way of implementing the composite key, now i'm concatenating 
> both fields into 1.
>
> *db.define_table('t_user_has_companies',*
> *                Field('unikey', notnull=True,required=True, 
> compute=lambda r: str(r.USER_ID) + str(r.COMP_ID)),*
> *                Field('USER_ID','reference 
> auth_user',label='User',writable=True,readable=True,notnull=True,required=True,ondelete='NO
>  
> ACTION'),*
> *                Field('COMP_ID','integer','reference 
> t_companies',label='Company',writable=True,readable=True,required=True,ondelete='NO
>  
> ACTION', requires=IS_IN_DB(db, db.t_companies, '%(id)s %(f_coname)s'))*
> *                )*
> *db.t_user_has_companies.unikey.requires = IS_NOT_IN_DB(db, 
> 't_user_has_companies.unikey')*
>
> But it's still allowing me to add duplicate concatenations. this is so 
> weird.
>
> Help please!!!
>
> *This e-mail is confidential and may also be privileged. If you are not 
> the intended **recipient, please notify the sender immediately, delete it 
> from your system and do **not copy, disseminate, distribute or disclose 
> any information contained therein.*
>

-- 
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/groups/opt_out.

Reply via email to