0

I would like to define the following table with a non identical field value as a follower should not follow himself :

db.define_table('followers', 
    Field('follower', 
          db.auth_user, 
          requires=(db.subscription.follower != db.subscription.user)),
    Field('user', 
          db.auth_user, 
          requires=(db.subscription.follower != db.subscription.user))
)

But I don't know how to implement it. Any hint ?

Thank you

John Doe
  • 1,570
  • 3
  • 13
  • 22

1 Answers1

1

Assuming inserts will happen via form processing:

db.define_table('followers', 
    Field('follower', 'reference auth_user',
          requires=IS_IN_DB(db(db.auth_user.id != request.post_vars.user),
                            'auth_user.id', db.auth_user._format)),
    Field('user', 'reference auth_user'))

This will allow any auth_user ID in the user field, but will limit the follower field to IDs other than the one submitted in request.post_vars.user. When there is no form submission, request.post_vars.user will simply be None, which doesn't matter because the validator is used only upon form submission.

Alternatively, you can use an onvalidation callback function when processing the form.

Anthony
  • 25,466
  • 3
  • 28
  • 57
  • I was looking for a database validator but your solution makes more sense as this is an user request feature. Thanks a lot ! – John Doe Oct 21 '15 at 16:26
  • Both fields still use the `IS_IN_DB` database validator (the validator for the `user` field is added by default given that it is a reference field). However, it is only necessary to limit one of the two to a subset of the `auth_user` records. – Anthony Oct 21 '15 at 16:43