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 9177777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:24:12+00:00 2026-06-17T17:24:12+00:00

I am writing a Flask/SQLAlchemy application in which I have users and groups. Users

  • 0

I am writing a Flask/SQLAlchemy application in which I have users and groups.

Users can belong to several groups, and they have a unique number within each group. Asking about how to model the database I was advised to use the following table structure for my many-to-many relationship:

TABLE UserGroups
  GroupID 
  UserID
  UserNumber
  PRIMARY KEY (GroupID, UserID)
  UNIQUE (GroupID, UserNumber)
  FOREIGN KEY (GroupID)
    REFERENCES Groups (GroupID)
  FOREIGN KEY (UserID)
    REFERENCES Users (UserID)

Now I know how to create a regular many-to-many relationship with SQLAlchemy, but I don’t know how to represent the UNIQUE constraint with the additional UserNumber field.

I don’t have a lot of experience with database design, ORMs and SQLAlchemy, so this may be obvious, but I can’t find a way to express it.

On of the things I don’t get is: using a regular many-to-many relationship, my User class has a list-like attribute groups which contains all the groups he belongs to, but this completely hides the UserGroups joining-table and I don’t know how to access the UserNumber field.

This is all a bit blur to me. Do you have any good example or explanations on how-to do such a thing with SQLAlchemy ?

  • 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-06-17T17:24:13+00:00Added an answer on June 17, 2026 at 5:24 pm

    The first part of the question (about creating a unique constraint with multiple columns) is already answered by cleg.

    However, the default many-to-many approach doesn’t work if you want to have additionally columns in the mapping table. Instead, you should use the Association Object Pattern. Additionally, you can simplify the access between user and group with an association_proxy.

    The proxied_association.py from the SQLAlchemy examples should be a good place to start.

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

Sidebar

Related Questions

I am writing a Flask site for which I would like to have routes
We're writing a flash application that can download a MP3 file, convert it to
I have a flask application that is setting up a database connection in a
I am writing my website's backend using Flask and Python 2.7, and have run
I'm writing an application with Flask and I'd like to generate different code for
I have a Python Flask app I'm writing, and I'm about to start on
I am writing an page inside Sitecore where users can create Sitecore items by
I'm writing a Python web application using Flask. My application establishes a connection to
I am writing an app in which users will be able to store information
I am writing a Flex (Flash Builder) application which has a map component (http://help.arcgis.com/en/webapi/flex/apiref/com/esri/ags/Map.html)

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.