Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7644723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:45:34+00:00 2026-05-31T09:45:34+00:00

I have a table defined in web2py db.define_table( ‘pairing’, Field(‘user’,writable=True,readable=True), Field(‘uid’, writable=True , readable=True)

  • 0

I have a table defined in web2py

db.define_table(
'pairing',
Field('user',writable=True,readable=True),
Field('uid', writable=True , readable=True)
)

This table needs to have user and uid combination being unique. I have looked through the web2py documentation , but there isn’t direct way to define composite key .
How do we define composite way in web2py

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:45:35+00:00Added an answer on May 31, 2026 at 9:45 am

    It depends on what you are trying to do. By default, web2py automatically creates an auto-incrementing id field to serve as the primary key for each table, and that is the recommended approach whenever possible. If you are dealing with a legacy database with composite primary keys and cannot change the schema, you can specify a primarykey attribute, though with some limitations (as explained here):

    db.define_table('pairing', 
        Field('user', writable=True, readable=True), 
        Field('uid', writable=True, readable=True),
        primarykey=['user', 'uid'])
    

    Perhaps instead you don’t really need a true composite primary key, but you just need some way to ensure only unique pairs of user/uid values are inserted in the table. In that case, you can do so by specifying a properly constructed IS_NOT_IN_DB validator for one of the two fields:

    db.define_table('pairing',
        Field('user', writable=True, readable=True),
        Field('uid', writable=True, readable=True))
    
    db.pairing.uid.requires=IS_NOT_IN_DB(db(db.pairing.user==request.vars.user),
        'pairing.uid')
    

    That will make sure uid is unique among the set of records where user matches the new value of user being inserted (so the combination of user and uid must be unique). Note, validators (such as IS_NOT_IN_DB) are only applied when values are being inserted via a SQLFORM or using the .validate_and_insert() method, so the above won’t work for arbitrary inserts into the table but is primarily intended for user input submissions.

    You can also use SQL to set a multi-column unique constraint on the table (which you can do directly in the database or via the web2py .executesql() method). Even with such a constraint, though, you would still want to do some input validation within your application to avoid errors from the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table field defined as like this approved_at DATETIME YEAR TO SECOND NOT
I have this model in web2py DAL: db.define_table('category', Field('name','string'), format='%(name)s' ) db.define_table('uploaded_question', Field('text','string'), ...
I have a User table defined like this: CREATE TABLE Users( UserId int IDENTITY(1,1)
I have a declarative table defined like this: class Transaction(Base): __tablename__ = transactions id
We have a composite primary key for the site table defined below. Functionally, this
I have an user defined table function in SQL Server that aggregate data from
I have a user-defined table type. I want to check it's existence before editing
I have a table defined like this: create table users ( id int(10), age
I have a table defined like this: Column: Version Message Type: varchar(20) varchar(100) ----------------------------------
In sqlalchemy 0.5 i have a table defined like this one: orders = Table('orders',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.