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

  • Home
  • SEARCH
  • 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 3302026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:48:36+00:00 2026-05-17T20:48:36+00:00

This question is about how to design a SQL relationship. I am pretty newbie

  • 0

This question is about how to design a SQL relationship. I am pretty newbie in this matter and I’d like to know the answers of (way) more experts guys…

I am currently migrating a ZopeDB (Object oriented) database to MySQL (relational) using MeGrok and SqlAlchemy (although I don’t think that’s really too relevant, since my question is more about designing a relationship in a relational database).

I have two classes related like this:

class Child(object):
    def __init__(self):
        self.field1 = "hello world"

class Parent(object):
    def __init__(self):
        self.child1 = Child()
        self.child2 = Child()

The “Parent” class has two different instances of a Child() class. I am not even sure about how to treat this (two different 1:1 relationships or a 1:2 relationship).

Currently, I have this:

class Child(rdb.Model):
    rdb.metadata(metadata)
    rdb.tablename("children_table")
    id = Column("id", Integer, primary_key=True)
    field1 = Column("field1", String(64))   #Irrelevant
    def __init__(self):
        self.field1 = "hello world"

class Parent(rdb.Model):
    rdb.metadata(metadata)
    rdb.tablename("parent_table")

    id = Column("id", Integer, primary_key=True)
    child1_id = Column("child_1_id", Integer, ForeignKey("children_table.id"))
    child2_id = Column("child_2_id", Integer, ForeignKey("children_table.id"))

    child1 = relationship(Child,
        primaryjoin = ("parent_table.child1_id == children_table.id")
    )

    child2 = relationship(Child,
        primaryjoin = ("parent_table.child2_id == children_table.id")
    ) 

Meaning… Ok, I store the two “children” ids as foreign keys in the Parent and retrieve the children itself using that information.

This is working fine, but I don’t know if it’s the most proper solution.

Or I could do something like:

class Child(rdb.Model):
    rdb.metadata(metadata)
    rdb.tablename("children_table")
    id = Column("id", Integer, primary_key=True)
    parent_id = Column("id", Integer, ForeignKey("parent_table.id"))  # New!
    type = Column("type", ShortInteger) # New!

    field1 = Column("field1", String(64))  #Irrelevant
    def __init__(self):
        self.field1 = "hello world"

class Parent(rdb.Model):
    rdb.metadata(metadata)
    rdb.tablename("parent_table")

    id = Column("id", Integer, primary_key=True)
    child1 = relationship(
        # Well... this I still don't know how to write it down,
        # but it would be something like:
        #   Give me all the children whose "parent_id" is my own "id"
        #   AND their type == 1
        # I'll deal with the joins and the actual implementation depending 
        # on your answer, guys
    )

    child2 = relationship(
        #  Would be same as above
        #  but selecting children whose type == 2
    )

This may be good for adding new children to the parent class… If I add a “Parent.child3”, I just need to create a new relationship very similar to the already existing ones.

The way I have it now would imply creating a new relationship AND adding a new foreign key to the parent.

Also, having a “parent” table with a bunch of foreign keys may not make it the best “parent” table in the world, right?

I’d like to know what people that know much more about databases think 🙂

Thank you.

PS: Related post? Question 3998545

  • 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-17T20:48:37+00:00Added an answer on May 17, 2026 at 8:48 pm

    I’ll admit that I’m not too familiar with object databases, but in relational terms this is a straightforward one-to-many (optional) relationship.

    create table parent (
      id           int PK,
      otherField   whatever
    )
    
    create table child (
      id           int PK,
      parent_id    int Fk,
      otherField   whatever
    )
    

    Obviously, that’s not usable code as it stands….

    I think this is similar to your second example. If you need to track the ordinal postion of the children in their relationships to the parent, you’d add a column to the child table such as:

    create table child (
      id           int PK,
      parent_id    int Fk,
      birth_order  int,
      otherField   whatever
    )
    

    You’d have to be responsible for managing that field at teh application level, it’s not something you can expect the DBMS to do for you.

    I called it an optional relationship on the assumption that childless parents can exist–if that’s not true, it becomes a required relationship logically, though you’d still have to let the DBMS create a new parent record childlessly, then grab its id to create the child–and once again manage the requirement at the application level.

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

Sidebar

Related Questions

I've been thinking about this object oriented design question for a while now and
I just came across this question about initializing local variables. Many of the answers
This question is a follow up to my previous question about getting the HTML
I was reading this question about how to parse URLs out of web pages
I have a question about this question . I posted a reply there but
As kind of a follow up to this question about prefixes , I agree
This question is about removing sequences from an array, not duplicates in the strict
This question is about organizing the actual CSS directives themselves within a .css file.
This question is about App domains and Sessions. Is it possible to have IIS
This question is about using getter methods of a singleton object in worker threads.

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.