I want to retrieve from my DB a single record, which is a question (I am building a quiz mechanism). I have the following requirements to this hit:
It should be a question from the quiz the user is currently playing
It should be a question which the user has not answered before, eg; it should not be a record in the responses table
The questions should be ordered by category.
The following models are involved:
Questions: Contains all questions from all quizzes
class Questions(db.Model):
# table name
__tablename__ = "questions"
id = db.Column(db.Integer, primary_key=True)
quiz_id = db.Column(db.Integer, ForeignKey("quizzes.id"))
text = db.Column(db.String(80))
category = db.Column(db.Integer)
type = db.Column(db.String(80))
Responses:Contains all responses
class Responses(db.Model):
# table name
__tablename__ = "responses"
id = db.Column(db.Integer, primary_key=True)
session_id = db.Column(db.Integer, ForeignKey("sessions.id"))
question_id = db.Column(db.Integer, ForeignKey("questions.id"))
option_id = db.Column(db.Integer, ForeignKey("options.id"))
answer = db.Column(db.String(80), nullable=True)
Responses: When a user starts a quiz, a session is started. This sessions relates to the quiz being played in order to be able to collect the question, and store and relate the responses to a user.
class Sessions(db.Model):
__tablename__ = "sessions"
id = db.Column(db.Integer, primary_key=True)
quiz_id = db.Column(db.Integer, ForeignKey("quizzes.id"))
user_id = db.Column(db.Integer, ForeignKey("users.id"))e here
For the first and third requirement I have written to following code:
question = Questions.query.filter(Questions.quiz_id == quiz.id).order_by(Questions.category).first()
This works in the sense that it returns the questions in te correct order
For the second requirement I have written the following:
check = Responses.query.filter(Responses.session_id == session[0].id,
Responses.question_id == question.id).scalar() is not None
Next I attempted to create one query, but this was not succesfull. See below code:
question = db.session.query(Questions, Responses).filter(Questions.quiz_id == quiz.id).filter(
Responses.session_id == session[0].id, Responses.question_id == Questions.id).order_by(Questions.category).scalar() is None
The result was it actually only gave me questions which were already answered, instead of unanswered. I am also not sure it's going to filter on the correct question_id already.
EDIT: I think I know have the correct SQL call
SELECT * FROM quiz.questions JOIN quiz.quizzes ON quiz.questions.quiz_id = quiz.quizzes.id WHERE quiz.quizzes.year = 2020 AND quiz.questions.id NOT IN (SELECT question_id FROM quiz.responses WHERE quiz.responses.session_id = 11 ) ORDER BY quiz.questions.category ASC;
You can use except to combine two queries; this will run both queries, and exclude the results of the second query (ie query1 questions - query2 questions):
For example:
# get questions from the current quiz
Questions.query.filter(Questions.quiz_id == quiz.id).\
except(
# except for questions with a response in the current session
Questions.query.filter(
Questions.quiz_id == quiz.id,
Questions.id == Responses.question_id,
Responses.session_id == session[0].id
)
).order_by(Questions.category).first()
In the end, this was the solution:
question = Questions.query\
.filter(Questions.quiz_id == quiz.id) \
.filter(Responses.session_id == session[0].id) \
.filter(Responses.question_id != Questions.id) \
.order_by(Questions.category) \
.order_by(func.rand()) \
.first()
Related
In the case of many-to-many relationships, an association table can be used in the form of Association Object pattern.
I have the following setup of two classes having a M2M relationship through UserCouncil association table.
class Users(Base):
name = Column(String, nullable=False)
email = Column(String, nullable=False, unique=True)
created_at = Column(DateTime, default=datetime.utcnow)
password = Column(String, nullable=False)
salt = Column(String, nullable=False)
councils = relationship('UserCouncil', back_populates='user')
class Councils(Base):
name = Column(String, nullable=False)
created_at = Column(DateTime, default=datetime.utcnow)
users = relationship('UserCouncil', back_populates='council')
class UserCouncil(Base):
user_id = Column(UUIDType, ForeignKey(Users.id, ondelete='CASCADE'), primary_key=True)
council_id = Column(UUIDType, ForeignKey(Councils.id, ondelete='CASCADE'), primary_key=True)
role = Column(Integer, nullable=False)
user = relationship('Users', back_populates='councils')
council = relationship('Councils', back_populates='users')
However, in this situation, suppose I want to search for a council with a specific name cname for a given user user1. I can do the following:
for council in user1.councils:
if council.name == cname:
dosomething(council)
Or, alternatively, this:
session.query(UserCouncil) \
.join(Councils) \
.filter((UserCouncil.user_id == user1.id) & (Councils.name == cname)) \
.first() \
.council
While the second one is more similar to raw SQL queries and performs better, the first one is simpler. Is there any other, more idiomatic way of expressing this query which is better performing while also utilizing the relationship linkages instead of explicitly writing traditional joins?
First, I think even the SQL query you bring as an example might need to go to fetch the UserCouncil.council relationship again to the DB if it is not loaded in the memory already.
I think that given you want to search directly for the Council instance given its .name and the User instance, this is exactly what you should ask for. Below is the query for that with 2 options on how to filter on user_id (you might be more familiar with the second option, so please use it):
q = (
select(Councils)
.filter(Councils.name == councils_name)
.filter(Councils.users.any(UserCouncil.user_id == user_id)) # v1: this does not require JOIN, but produces the same result as below
# .join(UserCouncil).filter(UserCouncil.user_id == user_id) # v2: join, very similar to original SQL
)
council = session.execute(q).scalars().first()
As to making it more simple and idiomatic, I can only suggest to wrap it in a method or property on the User instance:
class Users(...):
...
def get_council_by_name(self, councils_name):
q = (
select(Councils)
.filter(Councils.name == councils_name)
.join(UserCouncil).filter(with_parent(self, Users.councils))
)
return object_session(self).execute(q).scalars().first()
so that you can later call it user.get_council_by_name('xxx')
Edit-1: added SQL queries
v1 of the first q query above will generate following SQL:
SELECT councils.id,
councils.name
FROM councils
WHERE councils.name = :name_1
AND (EXISTS
(SELECT 1
FROM user_councils
WHERE councils.id = user_councils.council_id
AND user_councils.user_id = :user_id_1
)
)
while v2 option will generate:
SELECT councils.id,
councils.name
FROM councils
JOIN user_councils ON councils.id = user_councils.council_id
WHERE councils.name = :name_1
AND user_councils.user_id = :user_id_1
I have a scenario to iterate up session_number column for related user_name. If a user created a session before I'll iterate up the last session_number but if a user created session for the first time session_number should start from 1. I tried to illustrate on below. Right now I handle this by using logic but try to find more elegant way to do that in SqlAlchemy.
id - user_name - session_number
1 user_1 1
2 user_1 2
3 user_2 1
4 user_1 3
5 user_2 2
Here is my python code of the table. My database is PostgreSQL and I'm using alembic to upgrade tables. Right now it continues to iterate up the session_number regardless user_name.
class UserSessions(db.Model):
__tablename__ = 'user_sessions'
id = db.Column(db.Integer, primary_key=True, unique=True)
username = db.Column(db.String, nullable=False)
session_number = db.Column(db.Integer, Sequence('session_number_seq', start=0, increment=1))
created_at = db.Column(db.DateTime)
last_edit = db.Column(db.DateTime)
__table_args__ = (
db.UniqueConstraint('username', 'session_number', name='_username_session_number_idx_'),
)
I've searched on the internet for this situation but those were not like my problem. Is it possible to achieve this with SqlAlchemy/PostgreSQL actions?
First, I do not know of any "pure" solution for this situation by using either SqlAlchemy or Postgresql or a combination of the two.
Although it might not be exactly the solution you are looking for, I hope it will give you some ideas.
If you wanted to calculate the session_number for the whole table without it being stored, i would use the following query or a variation of thereof:
def get_user_sessions_with_rank():
expr = (
db.func.rank()
.over(partition_by=UserSessions.username, order_by=[UserSessions.id])
.label("session_number")
)
subq = db.session.query(UserSessions.id, expr).subquery("subq")
q = (
db.session.query(UserSessions, subq.c.session_number)
.join(subq, UserSessions.id == subq.c.id)
.order_by(UserSessions.id)
)
return q.all()
Alternatively, I would actually add a column_property to the model compute it on the fly for each instance of UserSessions. it is not as efficient in calculation, but for queries filtering by specific user it should be good enough:
class UserSessions(db.Model):
__tablename__ = "user_sessions"
id = db.Column(db.Integer, primary_key=True, unique=True)
username = db.Column(db.String, nullable=False)
created_at = db.Column(db.DateTime)
last_edit = db.Column(db.DateTime)
# must define this outside of the model definition because of need for aliased
US2 = db.aliased(UserSessions)
UserSessions.session_number = db.column_property(
db.select(db.func.count(US2.id))
.where(US2.username == UserSessions.username)
.where(US2.id <= UserSessions.id)
.scalar_subquery()
)
In this case, when you query for UserSessions, the session_number will be fetched from the database, while being None for newly created instances.
Abstraction of my problem, I have 2 tables. A User table, and a Friendship table.
I'm trying to make a query to list all the users available to be added as friend to User 1, Alice, and also excluding herself, using SQLAlchemy.
Considering there could be a lot of friendships, to find Alice's friends:
friend_subquery = db.session.query(Friendship).filter_by(User_id=1).subquery()
Now I want all the users listed, except Alice, and her friends, Bob and Jack.
friends = (db.session.query(User).
filter(User.ID != 1).
outerjoin(friend_subquery,
User.ID != friend_subquery.c.Friend_id))
My expected result would have been to get User 4 and 5, but this query
returns all except Alice herself. The condition of
User.ID != friend_subquery.c.Friend_id
seem NOT to be working as expected.
P.S. I've done my homework of searching, reading docs, but couldn't figure it out. Thanks for your time.
I assumed that your models are defined as below:
class User(db.Model):
__tablename__ = 'User'
ID = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
friendships = db.relationship(
'Friendship',
foreign_keys='Friendship.User_id',
backref='friender',
)
friendships_of = db.relationship(
'Friendship',
foreign_keys='Friendship.Friend_id',
backref='friendee',
)
class Friendship(db.Model):
__tablename__ = 'Friendship'
ID = db.Column(db.Integer, primary_key=True)
User_id = db.Column(db.Integer, db.ForeignKey('User.ID'))
Friend_id = db.Column(db.Integer, db.ForeignKey('User.ID'))
In which case two ways to perform this query is shown in the code below. The first query relies on the relationship User.friendships_of, while the second works with explicit joins:
# Add users
u1, u2, u3, u4, u5 = users = [
User(name="Alice"),
User(name="Bob"),
User(name="Jack"),
User(name="Pluto"),
User(name="Mike"),
]
db.session.add_all(users)
# Add friendhips
u1.friendships.append(Friendship(friendee=u2))
u1.friendships.append(Friendship(friendee=u3))
db.session.commit()
# Find Alice
u_alice = db.session.query(User).filter(User.name == 'Alice').one()
# Query (version 1)
q = (
db.session.query(User)
.filter(~User.friendships_of.any(Friendship.User_id == u_alice.ID))
.filter(User.ID != u_alice.ID)
.all()
)
for x in q:
print(x)
# Query (version 2)
q = (
db.session.query(User)
.outerjoin(
Friendship,
db.and_(
u_alice.ID == Friendship.User_id,
User.ID == Friendship.Friend_id,
)
)
.filter(Friendship.ID == None)
.filter(User.ID != u_alice.ID)
.all()
)
for x in q:
print(x)
I am creating a website using Flask and SQLAlchemy. This website keeps track of classes that a student has taken. I would like to find a way to search my database using SQLAlchemy to find all unique classes that have been entered. Here is code from my models.py for Class:
class Class(db.Model):
__tablename__ = 'classes'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
body = db.Column(db.Text)
created = db.Column(db.DateTime, default=datetime.datetime.now)
user_email = db.Column(db.String(100), db.ForeignKey(User.email))
user = db.relationship(User)
In other words, I would like to get all unique values from the title column and pass that to my views.py.
Using the model query structure you could do this
Class.query.with_entities(Class.title).distinct()
query = session.query(Class.title.distinct().label("title"))
titles = [row.title for row in query.all()]
titles = [r.title for r in session.query(Class.title).distinct()]
As #van has pointed out, what you are looking for is:
session.query(your_table.column1.distinct()).all(); #SELECT DISTINCT(column1) FROM your_table
but I will add that in most cases, you are also looking to add another filter on the results. In which case you can do
session.query(your_table.column1.distinct()).filter_by(column2 = 'some_column2_value').all();
which translates to sql
SELECT DISTINCT(column1) FROM your_table WHERE column2 = 'some_column2_value';
First, the database overview:
competitors - people who compete
competitions - things that people compete at
competition_registrations - Competitors registered for a particular competition
event - An "event" at a competition.
events_couples - A couple (2 competitors) competing in an event.
First, EventCouple, a Python class corresponding to events_couples, is:
class EventCouple(Base):
__tablename__ = 'events_couples'
competition_id = Column(Integer, ForeignKey('competitions.id'), primary_key=True)
event_id = Column(Integer, ForeignKey('events.id'), primary_key=True)
leader_id = Column(Integer)
follower_id = Column(Integer)
__table_args__ = (
ForeignKeyConstraint(['competition_id', 'leader_id'], ['competition_registrations.competition_id', 'competition_registrations.competitor_id']),
ForeignKeyConstraint(['competition_id', 'follower_id'], ['competition_registrations.competition_id', 'competition_registrations.competitor_id']),
{}
)
I have a Python class, CompetitorRegistration, that corresponds to a record/row in competition_registrations. A competitor, who is registered, can compete in multiple events, but either as a "leader", or a "follower". I'd like to add to CompetitorRegistration an attribute leading, that is a list of EventCouple where the competition_id and leader_id match. This is my CompetitorRegistration class, complete with attempt:
class CompetitorRegistration(Base):
__tablename__ = 'competition_registrations'
competition_id = Column(Integer, ForeignKey('competitions.id'), primary_key=True)
competitor_id = Column(Integer, ForeignKey('competitors.id'), primary_key=True)
email = Column(String(255))
affiliation_id = Column(Integer, ForeignKey('affiliation.id'))
is_student = Column(Boolean)
registered_time = Column(DateTime)
leader_number = Column(Integer)
leading = relationship('EventCouple', primaryjoin=and_('CompetitorRegistration.competition_id == EventCouple.competition_id', 'CompetitorRegistration.competitor_id == EventCouple.leader_id'))
following = relationship('EventCouple', primaryjoin='CompetitorRegistration.competition_id == EventCouple.competition_id and CompetitorRegistration.competitor_id == EventCouple.follower_id')
However, I get:
ArgumentError: Could not determine relationship direction for primaryjoin
condition 'CompetitorRegistration.competition_id == EventCouple.competition_id
AND CompetitorRegistration.competitor_id == EventCouple.leader_id', on
relationship CompetitorRegistration.leading. Ensure that the referencing Column
objects have a ForeignKey present, or are otherwise part of a
ForeignKeyConstraint on their parent Table, or specify the foreign_keys parameter
to this relationship.
Thanks for any help, & let me know if more info is needed on the schema.
Also, another attempt of mine is visible in following — this did not error, but didn't give correct results either. (It only joined on the competition_id, and completely ignored the follower_id)
Your leading's condition mixes expression and string to be eval()ed. And following's condition mixes Python and SQL operators: and in Python is not what you expected here. Below are corrected examples using both variants:
leading = relationship('EventCouple', primaryjoin=(
(competition_id==EventCouple.competition_id) & \
(competitor_id==EventCouple.leader_id)))
leading = relationship('EventCouple', primaryjoin=and_(
competition_id==EventCouple.competition_id,
competitor_id==EventCouple.leader_id))
following = relationship('EventCouple', primaryjoin=\
'(CompetitorRegistration.competition_id==EventCouple.competition_id) '\
'& (CompetitorRegistration.competitor_id==EventCouple.follower_id)')