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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:40:26+00:00 2026-06-01T12:40:26+00:00

I’m rusty in SQL and completely new to SQL Alchemy , but have an

  • 0

I’m rusty in SQL and completely new to SQL Alchemy, but have an upcoming project which uses both. So I thought I write something to get comfortable. Suffering from a hangover I decided to write something to keep track of alcohol levels.

I have events where users participate and consume drinks. Those are my three basic tables (with one helper table guestlist, for a m:n relationship between users and events).

drinks list the drinks available at all events to all users all the time (no need to map anything). users are created from time to time, so are events. all users can join all events, so I use the guestlist table to map those.

Now to the heart of the question: I need to keep track at what time which user consumes which drink on which event. I try to solve this whit another table shots (see below) but I’m not sure if this a good solution.

ERP Diagram
(source: anyimg.com)

In SQL Alchemy it might look somewhat like this (or not, but this is what I came up with so far)::

guestlist_table = Table('guestlist', Base.metadata,
    Column('event_id', Integer, ForeignKey('events.id')),
    Column('user_id', Integer, ForeignKey('users.id'))
)

class Event(Base):

  __tablename__ = 'events'

  id = Column(Integer, primary_key=True)
  name = Column(String(80), nullable=False)
  started = Column(DateTime, nullable=False, index=True,
    default=datetime.datetime.now
  )
  users = relationship("User", secondary=guestlist_table, backref="events")
  # ...

class User(Base):

  __tablename__ = 'users'

  id = Column(Integer, primary_key=True)
  name = Column(String(50), nullable=False, unique=True, index=True)
  birthdate = Column(Date, nullable=False)
  weight = Column(Integer, nullable=False)
  sex = Column(Boolean, nullable=False)
  # ...

class Drink(Base):

  __tablename__ = 'drinks'

  id = Column(Integer, primary_key=True)
  name = Column(String(50), nullable=False)
  volume = Column(Numeric(5,2), nullable=False)
  percent = Column(Numeric(5,2), nullable=False)
  # ...

class Shots(Base):

  __tablename__ = 'shots'

  id = Column(Integer, primary_key=True)
  at = Column(DateTime, nullable=False,
    default=datetime.datetime.now
  )
  user_id = Column(Integer, ForeignKey('users.id'), index=True)
  event_id = Column(Integer, ForeignKey('events.id'), index=True) 
  drink_id = Column(Integer, ForeignKey('drinks.id'))
  user = relationship("User", backref="shots")
  event = relationship("Event", backref="shots")
  drink = relationship("Drink", uselist=False) # one-to-one, no backref needed

I struggle to find a good way to build a table that maps events, users and drinks together: How should I formulate the relationships and how do I query it?

The thing is I kinda feel I overlooked something.
And frankly I’m absolute lost on how to query it?

Here are the queries I would make most of the time:

  • I at least need to get all the shots consumed on a event (probably sorted by user)
  • I also sometimes need all shots for a specific user (probably sorted by event)
  • And a lot of counting:
    • Number of shots per event
    • Number of shots per user
    • Number of shots a user downed at a event

Is the shots table a okay way to manage this?

  • 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-01T12:40:28+00:00Added an answer on June 1, 2026 at 12:40 pm

    I suggest you to draw an Entity Relationship Diagram, it would be much more clear for people and you to understand your question.

    To answer your question:

    I at least need to get all the shots consumed on a event (probably sorted by user)

    To get all shots of an event, you could try

    session.query(Shots).filter_by(event_id=event_id)
    

    The event_id is the id of event you like to query. To store by user, you can try

    from sqlalchemy.sql.expression import desc, asc
    session.query(Shots) \
        .filter_by(event_id=event_id) \
        .order_by(asc(Shots.user_id))
    

    Of course, you may want to sort by attribute of an user, you could join user table.

    from sqlalchemy.sql.expression import desc, asc
    session.query(Shots) \
        .filter_by(event_id=event_id) \
        .join(User) \
        .order_by(asc(User.name))
    

    easy enough.

    I also sometimes need all shots for a specific user (probably sorted by event)

    Just like previous example

    Number of shots per event

    session.query(Shots) \
        .filter_by(event_id=event_id) \
        .count()
    

    I didn’t run they, I just wrote them here, but they should all work if I didn’t make a typo.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.