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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:46:36+00:00 2026-05-15T01:46:36+00:00

Hmm, the title was harder to formulate than I thought. Basically, I’ve got these

  • 0

Hmm, the title was harder to formulate than I thought.

Basically, I’ve got these simple classes mapped to tables, using SQLAlchemy. I know they’re missing a few items but those aren’t essential for highlighting the problem.

class Customer(object):
    def __init__(self, uid, name, email):
        self.uid = uid
        self.name = name
        self.email = email

    def __repr__(self):
        return str(self)

    def __str__(self):
        return "Cust: %s, Name: %s (Email: %s)" %(self.uid, self.name, self.email)  

The above is basically a simple customer with an id, name and an email address.

class Order(object):
    def __init__(self, item_id, item_name, customer):
        self.item_id = item_id
        self.item_name = item_name
        self.customer = None

    def __repr__(self):
        return str(self)

    def __str__(self):
        return "Item ID %s: %s, has been ordered by customer no. %s" %(self.item_id, self.item_name, self.customer)  

This is the Orders class that just holds the order information: an id, a name and a reference to a customer. It’s initialised to None to indicate that this item doesn’t have a customer yet. The code’s job will assign the item a customer.

The following code maps these classes to respective database tables.

# SQLAlchemy database transmutation
engine = create_engine('sqlite:///:memory:', echo=False)
metadata = MetaData()

customers_table = Table('customers', metadata,
    Column('uid', Integer, primary_key=True),
    Column('name', String),
    Column('email', String)
)


orders_table = Table('orders', metadata,
    Column('item_id', Integer, primary_key=True),
    Column('item_name', String),
    Column('customer', Integer, ForeignKey('customers.uid'))
)

metadata.create_all(engine)
mapper(Customer, customers_table)
mapper(Order, orders_table)  

Now if I do something like:

for order in session.query(Order):
    print order  

I can get a list of orders in this form:

Item ID 1001: MX4000 Laser Mouse, has been ordered by customer no. 12  

What I want to do is find out customer 12’s name and email address (which is why I used the ForeignKey into the Customer table). How would I go about it?

Updated based on my comment

I know this seems a little useless, but just for the sake of knowing: what would I have to do to keep the relationship uni-directional?

Also, if I had another foreign key into another table? How would I update the mapper?

  • 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-15T01:46:36+00:00Added an answer on May 15, 2026 at 1:46 am

    First of all, if you do pass customer to the Order constructur, then at least use it.
    I suggest, use a default value, but still allow assigning to a customer on creation as below:

    class Order(object):
        def __init__(self, item_id, item_name, customer=None):
            self.item_id = item_id
            self.item_name = item_name
            self.customer = customer # self.customer = None
        #...
    

    In order for your relationship to work, you have to initialize relationship
    between objects, FK is not enough. I suggest you change your code as following (extracts):

    orders_table = Table('orders', metadata,
        Column('item_id', Integer, primary_key=True),
        Column('item_name', String),
        Column('customer_id', Integer, ForeignKey('customers.uid')) # changed
    )
    
    # for bi-directional relationship use:
    mapper(Customer, customers_table, properties={
        'orders': relationship(Orders, backref='customer')
    })
    mapper(Orders, orders_table)
    # for uni-directional relationship use:
    mapper(Customer, customers_table)
    mapper(Orders, orders_table, properties={
        'customer': relationship(Customer)
    })
    #...
    

    Then you can navigate from order to customer, and back:

    print mycustomer.orders # in case of bi-directional
    print order.customer
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hmm, I feel my OP title sounds wrong, but I am not sure how
hmm i got a homework, its 2 hours and i still have no clue
Hmm. Instead of defanging input or using some kind of regex to remove tags,
Hmm not sure if the title of this makes any sense. I'm creating a
Hmm. I'm noticing Stack is a subclass of Vector, and I thought Vector and
Hmm i would have thought this would be a little more straightforward.. in my
Hmm... Title is a bit of a mouthful, but I'm really not sure which
[EDIT] Hmm. Perhaps this question should be titled what is the default user-input dialog
Hmm, sounds easy enough but after looking at the ones that come with StringTemplate,
Does anyone know of any HMM implementation in .net? I've some stuff that I

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.