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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:39:00+00:00 2026-05-27T10:39:00+00:00

I have a data model that i have some concerns over. Here it is:

  • 0

I have a data model that i have some concerns over. Here it is:

enter image description here

My concern is that it’s possible to assign an application to a member, and then assign a role that is from a different application to the member.

Now, I know I can put a constraint on this to ensure that doesn’t happen, but that seems like a bandaid. I would rather design the model so that a constraint was not required.

Can anyone suggest how to alter the model to ensure that a member can only be assigned roles from an application it is assigned to?

  • 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-27T10:39:01+00:00Added an answer on May 27, 2026 at 10:39 am

    Usually, you run into this kind of problem when you’ve split a key. Fixing that split key, then using overlapping foreign key constraints is usually what you’re looking for.

    create table cmember (
      cmemberid integer primary key,
      username varchar(15) not null,
      emailaddress varchar(64) not null
    );
    
    create table application (
      applicationid integer primary key,
      description varchar(50) not null
    );
    
    create table member_application (
      cmemberid integer not null references cmember (cmemberid),
      applicationid integer not null references application (applicationid),
      primary key (cmemberid, applicationid)
    );
    
    create table role (
      roleid integer primary key,
      rolename varchar(25) not null
    );
    
    create table crole (
      croleid integer not null references role (roleid),
      -- Include the application id in this table . . .
      applicationid integer not null references application (applicationid),
      -- and make it part of the primary key.
      primary key (croleid, applicationid)
    );
    
    create table member_role (
      cmemberid integer not null references cmember (cmemberid),
      croleid integer not null,
      applicationid integer not null,
      primary key (cmemberid, croleid, applicationid),
      -- Note the overlapping foreign key constraints.
      foreign key (croleid, applicationid) references crole (croleid, applicationid),
      foreign key (cmemberid, applicationid) references member_application (cmemberid, applicationid)
    );
    
    insert into cmember values (1, 'A', 'A@b.com');
    insert into cmember values (2, 'B', 'B@b.com');
    
    insert into application values (1, 'App 1');
    insert into application values (2, 'App 2');
    
    insert into member_application values (1, 1);
    insert into member_application values (2, 2);
    
    insert into role values (1, 'Admin');
    
    insert into crole values (1, 1);
    insert into crole values (1, 2);
    
    insert into member_role values (1, 1, 1);
    insert into member_role values (2, 1, 2);
    

    Member 1 is assigned only to application 1. So trying to insert a row that references application 2 should fail.

    insert into member_role values (1,1,2);
    ERROR:  insert or update on table "member_role" violates foreign key constraint "member_role_cmemberid_fkey1"
    DETAIL:  Key (cmemberid, applicationid)=(1, 2) is not present in table "member_application".
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains data from some model. This class has metadata
I have some configuration data that I'd like to model in code as so:
I have a data model that includes common columns like addedBy, editedby (user), addedDate,
I have an Entity Data Model that I have created, and its pulling in
My Techie Bretheren (and Sisteren, of course!), I have a LinqToSql data model that
I have a relationship in a Core Data model that feels like it wants
I have set up a Core Data model that includes an entity, Item with
I have a model that I'm trying to retrieve an array of data from,
I have an NSTreeController that manages an entity is a core data model and
In my core data model I have a Person entity that has a to

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.