SQLAlchemy: How to use order_by based on a calculated value? - python

So I have the following database model:
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
nickname = db.Column(db.String(64), nullable=False, unique=True)
...
tasks = db.relationship('Task', backref='author', lazy='dynamic')
def number_of_tries(self):
return Task.query.filter_by(user_id=self.id).count()
def correct_answers(self):
return Task.query.filter_by(user_id=self.id).filter_by(correct=True).count()
def percentage(self):
try:
return '{:.2%}'.format(self.correct_answers()/self.number_of_tries())
except ZeroDivisionError:
return None
def __repr__(self):
return '<User %r>' % (self.nickname)
class Task(db.Model):
id = db.Column(db.Integer, primary_key=True)
task_id = db.Column(db.Integer)
correct = db.Column(db.Boolean)
timestamp = db.Column(db.DateTime)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
def __repr__(self):
return '<%r %s>' % (self.task_id, self.correct)
Two tables, the first one storing User data, the second one in relation with the first, storing data about questions the User answers.
I would like to sort the users based on their success ratio, so I would do something like this:
User.query.order_by(User.percentage).all()
but it won't work. What am I doing wrong? Is it even possible to use order_by based on a method like that?

Even with SqlAlchemy, you have to think in sets of objects and their values. The query you want involves three different sets: Users, their correct answers and their total answers.
Want you want is a query like that (warning, that's just a sample, you could write it much better)
select userid, cor_count/ans_count from users
inner join (select userid, count(*) cor_count from answers where correct=true group by userid) as correct_answers on users.userid=correct_answers.userid
inner join (select userid, count(*) as ans_count from answers group by userid) as total_answers on total_answers.userid=users.userid
where users.userid='xxxx'
order by 2
so, you have to formulate this (somehow) in SqlAlchemy. A guideline would by something to that effect:
ans_q = session.query(Task.user_id, func.count(task.id).label('cnt')).group_by(Task.user_id)
corr_ans_q = session.query(Task.user_id, func.count(task.id).label('cnt')).filter(Task.correct).group_by(Task.user_id)
ans_q = alias(ans_q.selectable)
corr_ans_q = alias(corr_ans_q.selectable)
q = session.query(User).join(ans_q,ans_q.c.user_id==User.id).join(corr_ans_q.c.user_id==User.id).order_by(corr_ans_q.c.cnt/ans_q.c.cnt)

I think that "Hybrid Attributes" will solve you problem.
http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html#module-sqlalchemy.ext.hybrid
UPD:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
nickname = Column(String(64), nullable=False, unique=True)
tasks = relationship('Task', backref='author', lazy='dynamic')
def number_of_tries(self):
return self.tasks.count()
def correct_answers(self):
return self.tasks.filter_by(correct=True).count()
def percentage(self):
try:
return '{:.2%}'\
.format(float(self.correct_answers())/self.number_of_tries())
except ZeroDivisionError:
return None
def __repr__(self):
return '<User %r>' % (self.nickname)
class Task(Base):
__tablename__ = 'task'
id = Column(Integer, primary_key=True)
task_id = Column(Integer)
correct = Column(Boolean)
timestamp = Column(DateTime)
user_id = Column(Integer, ForeignKey('user.id'))
def __repr__(self):
return '<%r %s>' % (self.task_id, self.correct)
from sqlalchemy import create_engine
engine = create_engine('sqlite:///:memory:')
Base.metadata.create_all(bind=engine)
from sqlalchemy.orm import sessionmaker
# create a configured "Session" class
Session = sessionmaker(bind=engine)
# create a Session
session = Session()
from datetime import datetime
task1 = Task(task_id=1, correct=True, timestamp=datetime.now())
task2 = Task(task_id=2, correct=False, timestamp=datetime.now())
task3 = Task(task_id=3, correct=False, timestamp=datetime.now())
task4 = Task(task_id=4, correct=False, timestamp=datetime.now())
task5 = Task(task_id=5, correct=True, timestamp=datetime.now())
task6 = Task(task_id=6, correct=True, timestamp=datetime.now())
user1 = User(nickname="Lekha", tasks=[task1, task2, task3])
user2 = User(nickname="Vanya", tasks=[task4, task5, task6])
session.add(user1, user2)
session.commit()
print(user1.number_of_tries())
print(user1.correct_answers())
print(user1.percentage())

Related

SQLAlchemy - How to correctly connect two sets of data?

I am hoping for some guidance about what I believe is going to be a common pattern in SQLAlchemy for Python. However, I have so far failed to find a simple explanation for someone new to SQLAlchemy.
I have the follow objects:
Customers
Orders
Products
I am building a Python FastAPI application and I want to be able to create customers, and products individually. And subsequently, I want to then be able to create an order for a customer that can contain 1 or more products. A customer will be able to have multiple orders also.
Here are my SQLAlchemy models:
order_products = Table('order_products', Base.metadata,
Column('order_id', ForeignKey('orders.id'), primary_key=True),
Column('product_id', ForeignKey('products.id'), primary_key=True)
)
class Customer(Base):
__tablename__ = "customers"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
address = Column(String)
phonenumber = Column(String)
email = Column(String, unique=True, index=True)
is_active = Column(Boolean, default=True)
orders = relationship("Order", back_populates="customers")
class Order(Base):
__tablename__ = "orders"
id = Column(Integer, primary_key=True, index=True)
ordernumber = Column(String, index=True)
customer_id = Column(Integer, ForeignKey("customers.id"))
customers = relationship("Customer", back_populates="orders")
products = relationship("Product", secondary="order_products", back_populates="orders")
class Product(Base):
__tablename__ = "products"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
size = Column(Integer)
order_id = Column(Integer, ForeignKey("orders.id"))
orders = relationship("Order", secondary="order_products", back_populates="products")
And here are my CRUD operations:
def create_customer(db: Session, customer: customer.CustomerCreate):
db_customer = models.Customer(name = customer.name, address = customer.address, email=customer.email, phonenumber=customer.phonenumber)
db.add(db_customer)
db.commit()
db.refresh(db_customer)
return db_customer
def create_product(db: Session, product: product.Productreate):
db_product = models.Product(name = product.name, size = product.size)
db.add(db_product)
db.commit()
db.refresh(db_product)
return db_product
def create_order(db: Session, order: order.OrderCreate, cust_id: int):
db_order = models.Order(**order.dict(), customer_id=cust_id)
db.add(db_order)
db.commit()
db.refresh(db_order)
return db_order
def update_order_with_product(db: Session, order: order.Order):
db_order = db.query(models.Order).filter(models.Order.id==1).first()
if db_order is None:
return None
db_product = db.query(models.Order).filter(models.Product.id==1).first()
if db_order is None:
return None
db_order.products.append(db_product)
db.add(db_order)
db.commit()
db.refresh(db_order)
return db_order
All of the CRUD operations work apart from update_order_with_product which gives me this error:
child_impl = child_state.manager[key].impl
KeyError: 'orders'
I'm not sure if I am taking the correct approach to the pattern needed to define the relationships between my models. If not, can someone point me in the right direction of some good examples for a beginner?
If my pattern is valid then there must be an issue with my CRUD operation trying to create the relationships? Can anyone help with that?
This query could be a problem:
db_product = db.query(models.Order).filter(models.Product.id==1).first()
Should probably be:
db_product = db.query(models.Product).filter(models.Product.id==1).first()
because you want to get a Product instance, not Order.
When you update a record you should not add it to the session (because it has been registered to the session when you queried the record).
def update_order_with_product(db: Session, order: order.Order):
db_order = db.query(models.Order).filter(models.Order.id==1).first()
if db_order is None:
return None
db_product = db.query(models.Product).filter(models.Product.id==1).first()
if db_product is None:
return None
db_order.products.append(db_product)
db.commit()
db.refresh(db_order)
return db_order

cannot get one to many relationship working in (Flask-) SQLAlchemy

I have several classes:
import uuid
from app import db, create_app
from sqlalchemy.sql import func
from sqlalchemy.dialects.postgresql import UUID, ARRAY, JSONB
class Ticket(db.Model):
__tablename__ = 'tickets'
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
time = db.Column(db.DateTime, server_default=func.now(), index=True)
proposed_names = db.Column(ARRAY(db.String))
measurements = db.relationship('TempMeasurement', back_populates='ticket')
class BaseMeasurement(object):
id = db.Column(db.Integer, primary_key=True)
#declared_attr
def type_id(self):
return db.Column(db.Integer, db.ForeignKey('optical_data_types.id'))
#declared_attr
def type(self):
return db.relationship('OpticalDataType')
#declared_attr
def operator_id(self):
return db.Column(db.Integer, db.ForeignKey('operators.id'))
#declared_attr
def operator(self):
return db.relationship('Operator')
#declared_attr
def item_id(self):
return db.Column(db.String, db.ForeignKey('items.serial'))
#declared_attr
def item(self):
return db.relationship('Item')
time = db.Column(db.DateTime, index=True)
instrument = db.Column(db.String)
instrument_sn = db.Column(db.String)
data = db.Column(JSONB)
class TempMeasurement(db.Model, BaseMeasurement):
__tablename__ = 'ticket_data'
id = db.Column(db.Integer, primary_key=True)
ticket_id = db.Column(UUID(as_uuid=True), db.ForeignKey('tickets.id'), index=True)
ticket = db.relationship('Ticket', back_populates='measurements')
original_paths = db.Column(ARRAY(db.String))
What I want/expect is that I can create a Ticket with several child TempMeasurements and commit this to the database. Something like:
app = create_app()
with app.app_context():
ticket = Ticket()
ticket.measurements = [TempMeasurement(...)]
db.session.add(ticket) # <-- error on this line
db.session.commit()
However, I get an obscure error deep in SQLAlchemy:
AttributeError: 'str' object has no attribute '_sa_instance_state'
with a full trace here.
I thought that it might be because the UUID ticket_id column has as_uuid, so I made it simply UUID (implicitly a str), but this did not solve my issue.
The error is too deep in SQLAlchemy for me to understand -- can anyone help?

How do I get rid of a circular dependency error while creating a database in sqlalchemy?

I'm new at using sqlalchemy. How do I get rid of a circular dependency error for the tables shown below. Basically my goal is to create A question table with a one to one relationship "best answer" to answer and a one to many relationship "possible_answers" as well.
class Answer(Base):
__tablename__ = 'answers'
id = Column(Integer, primary_key=True)
text = Column(String)
question_id = Column(Integer, ForeignKey('questions.id'))
def __init__(self, text, question_id):
self.text = text
def __repr__(self):
return "<Answer '%s'>" % self.text
class Question(Base):
__tablename__ = 'questions'
id = Column(Integer, primary_key=True)
text = Column(String)
picture = Column(String)
depth = Column(Integer)
amount_of_tasks = Column(Integer)
voting_threshold = Column(Integer)
best_answer_id = Column(Integer, ForeignKey('answers.id'), nullable=True)
possible_answers = relationship("Answer", post_update=True, primaryjoin = id==Answer.question_id)
def __init__(self, text, picture, depth, amount_of_tasks):
self.text = text
self.picture = picture
self.depth = depth
self.amount_of_tasks = amount_of_tasks
def __repr__(self):
return "<Question, '%s', '%s', '%s', '%s'>" % (self.text, self.picture, self.depth, self.amount_of_tasks)
def __repr__(self):
return "<Answer '%s'>" % self.text
This is the error message:
CircularDependencyError: Circular dependency detected. Cycles:
Apparently SQLAlchemy does not play well with circular dependencies. You might consider using an association table instead to represent the best answer...
from sqlalchemy import Column, Integer, String, ForeignKey, create_engine
from sqlalchemy import Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
engine = create_engine('sqlite:///:memory:')
Base = declarative_base()
class Answer(Base):
__tablename__ = 'answer'
id = Column(Integer, primary_key=True)
question_id = Column(Integer, ForeignKey('question.id'))
text = Column(String)
question = relationship('Question', backref='answers')
def __repr__(self):
return "<Answer '%s'>" % self.text
class Question(Base):
__tablename__ = 'question'
id = Column(Integer, primary_key=True)
text = Column(String)
best_answer = relationship('Answer',
secondary=lambda: best_answer,
uselist=False)
def __repr__(self):
return "<Question, '%s'>" % (self.text)
best_answer = Table('best_answer', Base.metadata,
Column('question_id',
Integer,
ForeignKey('question.id'),
primary_key=True),
Column('answer_id',
Integer,
ForeignKey('answer.id'))
)
if __name__ == '__main__':
session = sessionmaker(bind=engine)()
Base.metadata.create_all(engine)
question = Question(text='How good is SQLAlchemy?')
somewhat = Answer(text='Somewhat good')
very = Answer(text='Very good')
excellent = Answer(text='Excellent!')
question.answers.extend([somewhat, very, excellent])
question.best_answer = excellent
session.add(question)
session.commit()
question = session.query(Question).first()
print(question.answers)
print(question.best_answer)
Mark's solution works, but I wanted to find a way to do it without creating an additional table. After extensive searching, I finally found this example in the docs:
http://docs.sqlalchemy.org/en/latest/orm/relationship_persistence.html (the 2nd example)
The approach is to use primaryjoin [1] on both relationships in the Question model, and to add post_update=True on one of them. The post_update tells sqlalchemy to set best_answer_id as an additional UPDATE statement, getting around the circular dependency.
You also need foreign_keys specified on the question relationship in the Answer model.
Below is Mark's code modified to follow the linked example above. I tested it with sqlalchemy v1.1.9.
from sqlalchemy import Column, Integer, String, ForeignKey, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
engine = create_engine('sqlite:///:memory:')
Base = declarative_base()
class Answer(Base):
__tablename__ = 'answer'
id = Column(Integer, primary_key=True)
text = Column(String)
question_id = Column(Integer, ForeignKey('question.id'))
question = relationship('Question', back_populates='answers', foreign_keys=[question_id])
def __repr__(self):
return "<Answer '%s'>" % self.text
class Question(Base):
__tablename__ = 'question'
id = Column(Integer, primary_key=True)
text = Column(String)
best_answer_id = Column(Integer, ForeignKey('answer.id'))
answers = relationship('Answer', primaryjoin= id==Answer.question_id)
best_answer = relationship('Answer', primaryjoin= best_answer_id==Answer.id, post_update=True)
def __repr__(self):
return "<Question, '%s'>" % (self.text)
if __name__ == '__main__':
session = sessionmaker(bind=engine)()
Base.metadata.create_all(engine)
question = Question(text='How good is SQLAlchemy?')
somewhat = Answer(text='Somewhat good')
very = Answer(text='Very good')
excellent = Answer(text='Excellent!')
question.answers.extend([somewhat, very, excellent])
question.best_answer = excellent
session.add(question)
session.commit()
question = session.query(Question).first()
print(question.answers)
print(question.best_answer)
[1] Interestingly, the "string format" for primaryjoin seems to cause an error -- but constructing the SQL expression with the overloaded operators on the column objects works.
The proper way seems to be ForeignKeyConstraint(..., use_alter=True).
http://docs.sqlalchemy.org/en/latest/core/constraints.html#sqlalchemy.schema.ForeignKeyConstraint.params.use_alter
You could also sort of 'decorate' your models once they're initially defined.
class Answer(Base):
__tablename__ = 'answers'
id = Column(Integer, primary_key=True)
text = Column(String)
class Question(Base):
__tablename__ = 'questions'
id = Column(Integer, primary_key=True)
text = Column(String)
picture = Column(String)
depth = Column(Integer)
amount_of_tasks = Column(Integer)
voting_threshold = Column(Integer)
best_answer_id = Column(Integer, ForeignKey('answers.id'), nullable=True)
Answer.question_id = Column(Integer, ForeignKey(Question.id))
Question.possible_answers = relationship(Answer, post_update=True, primaryjoin=Question.id==Answer.question_id)
It's not too nice as the class definition starts to float around a little but it does the trick.

Outer Join with sqlalchemy (ORM)

I am trying to do a relationship() with an OUTER JOIN so that it joins the second table if there is something to join it with. I am currently stuck on how to do this though, I cannot seem to figure out the right combination of options(), relationship() and outerjoin().
I have the following tables and I am trying to join AppLike to Application if a row exists with the Application ID AND the artistID (which is provided by the function)
Happy to provide any additional information, I already have one of my joins working as you can see below, but there will always be a row to match for that one.
from sqlalchemy import Column
from . import Base
from . import DBSession
from sqlalchemy.dialects.mysql import (
INTEGER,
VARCHAR,
TEXT,
TINYINT,
)
from sqlalchemy.sql import and_
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship, joinedload
import time
# 0 = new
# 1 = Denied
# 2 = Accepted
def getNewApplications(artistID):
query = DBSession.query(Application).\
options(joinedload('pieces')).\
options(joinedload('vote')).\
filter(AppLike.artist_id==artistID).\
filter(Application.approved==0)
#join(AppPiece, Application.app_id==AppPiece.app_id).\
#outerjoin(AppLike, and_(Application.app_id==AppLike.app_id,
# AppLike.artist_id==artistID)).\
import pdb; pdb.set_trace()
return query.all()
class Application(Base):
""" The SQLAlchemy declarative model class for a FileFavorite object. """
__tablename__ = 'applications'
__table_args__ = {
'mysql_engine': 'InnoDB',
'mysql_charset': 'utf8'
}
app_id = Column(INTEGER(11), autoincrement=True, primary_key=True, nullable=False)
name = Column(VARCHAR(64), nullable=False)
nickname = Column(VARCHAR(64), nullable=False)
email = Column(VARCHAR(255), nullable=False)
description = Column(TEXT(), nullable=False)
profile_link = Column(VARCHAR(128), nullable=False)
location = Column(VARCHAR(64), nullable=False)
approved = Column(TINYINT(4), nullable=False)
pieces = relationship("AppPiece", lazy='joined')
vote = relationship("AppLike", lazy='joined')
def __init__(self, name, nickname, email, desc, profileLink,
location, approved):
self.name = name
self.nickname = nickname
self.email = email
self.description = desc
self.profile_link = profileLink
self.location = location
self.approved = approved
class AppPiece(Base):
""" The SQLAlchemy declarative model class for a FileFavorite object. """
__tablename__ = 'app_pieces'
__table_args__ = {
'mysql_engine': 'InnoDB',
'mysql_charset': 'utf8'
}
app_piece_id = Column(INTEGER(11), autoincrement=True, primary_key=True, nullable=False)
app_id = Column(INTEGER(11), ForeignKey('applications.app_id'))
link = Column(VARCHAR(128), nullable=False)
def __init__(self, appID, link):
self.app_id = appID
self.link = link
class AppLike(Base):
""" The SQLAlchemy declarative model class for a FileFavorite object. """
__tablename__ = 'app_likes'
__table_args__ = {
'mysql_engine': 'InnoDB',
'mysql_charset': 'utf8'
}
app_id = Column(INTEGER(11), ForeignKey('applications.app_id'))
artist_id = Column(INTEGER(11), primary_key=True, nullable=False)
vote = Column(TINYINT(4), nullable=False)
def __init__(self, appID, artistID, vote):
self.app_id = appID
self.artist_id = artistID
self.vote = vote
You definitely don't need options(joinedload('pieces')), it is already defined in your models (lazy='joined'). The join condition is the tricky part here and needs to be done using subquery, since we want to filter there as well. So, the final query should look something like this:
# We do the filtering on AppLike in the subquery and later join
# Application to it.
applike_subq = DBSession.query(AppLike).\
filter(AppLike.artist_id == artistID).subquery()
query = DBSession.query(Application).\
outerjoin(applike_subq, Application.vote).\
filter(Application.approved == 0).all()

How to extend the `getter`-functionality of SQLAlchemy's association_proxy?

Edit: I would like to model a 1 to 0:1 relationship between User and Comment (a User can have zero or one Comment). Instead of accessing the object Comment I would rather directly access the comment itself. Using SQLAlchemys association_proxy works perfect for that scenario except for one thing: accessing User.comment before having a Comment associated. But in this case I would rather expect None instead of AttributeError as result.
Look at the following example:
import sqlalchemy as sa
import sqlalchemy.orm as orm
from sqlalchemy import Column, Integer, Text, ForeignKey, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(Text)
def __init__(self, name):
self.name = name
# proxy the 'comment' attribute from the 'comment_object' relationship
comment = association_proxy('comment_object', 'comment')
class Comment(Base):
__tablename__ = 'comments'
id = Column(Integer, primary_key=True)
comment = Column('comment', Text, nullable=False, default="")
user_id = Column(ForeignKey('users.id'), nullable=False, unique=True)
# user_id has to be unique to ensure that a User can not have more than one comments
def __init__(self, comment):
self.comment = comment
user_object = orm.relationship(
"User",
uselist=False, # added after edditing the question
backref=orm.backref('comment_object', uselist=False)
)
if __name__ == "__main__":
engine = sa.create_engine('sqlite:///:memory:', echo=True)
Session = orm.sessionmaker(bind=engine)
Base.metadata.create_all(engine)
session = Session()
Now, the following code throws an AttributeError:
u = User(name="Max Mueller")
print u.comment
What would be the best way to catch that exception and provide a default value instead (like an empty string)?
You don't really need association_proxy for this. You could really get by just fine with a regular property. The AttributeError is (probably) caused because the comment_object is itself None, since there is no dependent row, and None has no comment attribute.
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(Text)
def __init__(self, name):
self.name = name
# proxy the 'comment' attribute from the 'comment_object' relationship
#property
def comment(self):
if self.comment_object is None:
return ""
else:
return self.comment_object.comment
#comment.setter
def comment(self, value):
if self.comment_object is None:
self.comment_object = Comment()
self.comment_object.comment = value
Try this
import sqlalchemy as sa
import sqlalchemy.orm as orm
from sqlalchemy import Column, Integer, Text, ForeignKey, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(Text)
def __init__(self, name):
self.name = name
# proxy the 'comment' attribute from the 'comment_object' relationship
comment = association_proxy('comment_object', 'comment')
class Comment(Base):
__tablename__ = 'comments'
id = Column(Integer, primary_key=True)
comment = Column('comment', Text, nullable=False, default="")
user_id = Column(ForeignKey('users.id'), nullable=False)
def __init__(self, comment):
self.comment = comment
user_object = orm.relationship(
"User",
backref=orm.backref('comment_object'),
uselist=False
)
if __name__ == "__main__":
engine = sa.create_engine('sqlite:///:memory:', echo=True)
Session = orm.sessionmaker(bind=engine)
Base.metadata.create_all(engine)
session = Session()
u = User(name="Max Mueller")
# comment = Comment("")
# comment.user_object = u
# session.add(u)
# session.commit()
print "SS :", u
print u.comment
You gave uselist in backref which must be in relationship.
I do not see any answer that would solve the issue and also would work with "sort_by" for example.
Maybe it is just better to use 'column_property", see Order by association proxy: invalid sql.

Categories

Resources