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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:42:49+00:00 2026-06-13T18:42:49+00:00

I want to create a flat forum, where threads are no separate table, with

  • 0

I want to create a flat forum, where threads are no separate table, with a composite primary key for posts.

So posts have two fields forming a natural key: thread_id and post_number, where the further is the ID of the thread they are part of, and the latter is their position in the thread. if you aren’t convinced, check below the line.

My problem is that i don’t know how to tell SQLAlchemy

when committing the addition of new Post instances with thread_id tid, look up how many posts with thread_id tid exist, and autoincrement from that number on.


Why do i think that schema is a good idea? because it’s natural and performant:

class Post(Base):
    number    = Column(Integer, primary_key=True, autoincrement=False, nullable=False)
    thread_id = Column(Integer, primary_key=True, autoincrement=False, nullable=False)
    title     = Column(Text) #nullable for not-first posts
    text      = Column(Text, nullable=False)
    ...
PAGESIZE = 10
#test
tid = 5
page = 4

Entire Thread (query):

thread5 = session.query(Post).filter_by(thread_id=5)

Thread title:

title = thread5.filter_by(number=0).one().title

Thread page

page4 = thread5.filter(
    Post.number >= (page    * PAGESIZE),
    Post.number < ((page+1) * PAGESIZE)).all()
#or
page4 = thread5.offset(page * PAGESIZE).limit(PAGESIZE).all()

Number of pages:

ceil(thread5.count() / PAGESIZE)
  • 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-13T18:42:50+00:00Added an answer on June 13, 2026 at 6:42 pm

    You can probably do this with an SQL expression as a default value (see the default argument). Give it a callable like this:

    from sqlalchemy.sql import func
    
    def maxnumber_for_threadid(context):
        return post_table.select([func.max(post_table.c.number)]).where(post_table.c.thread_id==context.current_parameters['thread_id'])
    

    I’m not absolutely sure you can return an sql expression from a default callable–you may have to actually execute this query and return a scalar value inside the callback. (The cursor should be available from the context parameter.)

    However, I strongly recommend you do what @kindall says and just use another auto-incrementing sequence for the number column. What you want to do is actually very tricky to get right even without SQLAlchemy. For example, if you are using an MVCC database you need to introduce special row-level locking so that the number of rows with a matching thread_id does not change while you are running the transaction. How this is done is database-dependent. For example with MySQL InnoDB, you need to do something like this:

    BEGIN TRANSACTION;
    SELECT MAX(number)+1 FROM posts WHERE thread_id=? FOR UPDATE;
    INSERT INTO posts (thread_id, number) VALUES (?, ?); -- number is from previous query
    COMMIT;
    

    If you didn’t use FOR UPDATE, then conceivably another connection trying to insert a new post into the same thread at the same time could have gotten the same value for number.

    So rather than being performant, post inserts are actually quite slow (relatively speaking) because of the extra query and locking required.

    All this is resolved by using a separate sequence and not worrying about post number incrementing only within a thread_id.

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

Sidebar

Related Questions

I want to create a new field (or two) in my table that is
I want to create two dimensional array of buttons in windows form. I have
I want to create a flat result set from the results of two methods
i want create image animation , i have 50 images with png format now
I want to create a flat button with rounded right top and bottom corners.
I have a flat array that I'm trying to make multidimensional. Basically, I want
I want to create flat links for my website. The .htaccess code is following:
Introduction: I have a flat ArrayCollection of object's, which i group to create the
I have an old Access database that is basically one flat file. I want
i want to create one ssis package which takes values from flat file and

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.