Skip to content

Instantly share code, notes, and snippets.

@illia-v
Created December 15, 2018 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save illia-v/b5af28cceabc4c64816b5b87cca03e49 to your computer and use it in GitHub Desktop.
Save illia-v/b5af28cceabc4c64816b5b87cca03e49 to your computer and use it in GitHub Desktop.
How to limit results of a database query per group using SQLAlchemy and PostgreSQL
from faker import Faker
fake = Faker()
for _ in range(3):
query = authors_table.insert().values(name=fake.name())
engine.execute(query)
result = engine.execute(authors_table.select())
for author_id in (author.id for author in result):
for _ in range(5):
query = articles_table.insert().values(
author_id=author_id,
title=fake.text(15)[:-1],
text=fake.text(20) + '..',
views=fake.pyint(),
)
engine.execute(query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment