0

I have 3 tables with next columns

Blog:
blog_id
title

BlogUser:
user_id   ForeignKey("User.user_id")
blog_id   ForeignKey("Blog.blog_id")
status    (read/unread)

BlogBody:
blog_id:  ForeignKey("Blog.blog_id")
page:     int
text:     VARCHAR(256)

The BlogBody table stores a blog text divided into 256-character pages. I want to write a query that will return me information about the blog, status and full text. The length of the full text can be different, but it is always divided into segments of 256 characters and page number.

I wrote something like this

query = db.query(Blog.blog_id, Blog.title, BlogUser.status, BlogBody.page, BlogBody.text)\
            .join(BlogUser)\
            .join(User)\
            .join(BlogBody, BlogBody.blog_id == Blog.blog_id,)

But here each segment of the full text is returned in a separate text in response:

[
{
  "blog_id": 1,
  "title": "One",
  "status": "unread",
  "page": 1,
  "text": "less than 256 characters"
},
{
  "blog_id": 2,
  "title": "Two",
  "status": "unread",
  "page": 1,
  "text": "... exactly 256 characters ..."
},
{
  "blog_id": 2,
  "title": "Two",
  "status": "unread",
  "page": 2,
  "text": "... exactly 256 characters ..."
},
{
  "blog_id": 2,
  "title": "Two",
  "status": "unread",
  "page": 3,
  "text": "... rest, less than 256 characters ..."
},
]

but I want:

[
{
  "blog_id": 1,
  "title": "One",
  "status": "unread",
  "page": 1,
  "text": "less than 256 characters"
},
{
  "blog_id": 2,
  "title": "Two",
  "status": "unread",
  "page": 1,
  "text": "... exactly 256 characters ... + ... exactly 256 characters ... + rest"
},
]

Is it possible to do this by sqlalchemy?

G Baz
  • 20
  • 3

0 Answers0