SQLAlchemy: Override relationship-defined "order_by" in a query - python

So, I have a model that is something like:
class Foo(model):
__tablename__ = "foo"
id = Column(Integer, primary_key=True)
data = relationship(
"FooData",
cascade="all, delete-orphan",
backref="foo",
lazy="dynamic",
order_by="desc(FooData.timestamp)"
)
#property
def first_item(self):
# the problem is here:
return self.data.order_by(asc("timestamp")).first()
#property
def latest_item(self):
return self.data.first()
class FooData(Model):
__tablename__ = "foo_data"
foo_id = Column(Integer, ForeignKey("foo.id"), primary_key=True)
timestamp = Column(DateTime, primary_key=True)
actual_data = Column(Float, nullable=False)
So, the problem is with the first_item method there: when it is defined as above, the SQL looks like this:
SELECT foo_data.timestamp AS foo_data_timestamp, foo_data.actual_data AS foo_data_actual_data, foo_data.foo_id AS foo_data_foo_id
FROM foo_data
WHERE :param_1 = foo_data.foo_id ORDER BY foo_data.timestamp DESC, foo_data.timestamp ASC
-- ^^^^^^^^^^^^^^^^^^^^^^
Obviously, the order_by specified in the query is being appended to the one specified in the relationship definition, instead of replacing it; is there a way for a query to override the original order_by? I know I could specify a separate query directly on the FooData class, but I would like to avoid that if possible.

According to documentation:
All existing ORDER BY settings can be suppressed by passing None - this will suppress any ORDER BY configured on mappers as well.
So the simple solution is to reset ORDER BY clause and then apply the one you need. Like:
self.data.order_by(None).order_by(asc("timestamp")).first()
In case you don't want to reset whole ORDER BY clause, but only want to override one column order, AFAIK there is no built-in way for it.

I know this is an old post, but it showed up when I was searching, so maybe this will be useful to someone else
class Foo(model):
__tablename__ = "foo"
id = Column(Integer, primary_key=True)
data = relationship(
"FooData",
cascade="all, delete-orphan",
backref="foo",
lazy="dynamic",
order_by=lambda: FooData.__table__.columns.timestamp.desc()
)
...
class FooData(Model):
__tablename__ = "foo_data"
foo_id = Column(Integer, ForeignKey("foo.id"), primary_key=True)
timestamp = Column(DateTime, primary_key=True)
actual_data = Column(Float, nullable=False)

Related

SQLAlchemy: #property mapping?

I have following models:
class Details(db.Model):
details_id = db.Column(db.Integer, primary_key=True)
details_main = db.Column(db.String(50))
details_desc = db.Column(db.String(50))
class Data(db.Model):
data_id = db.Column(db.Integer, primary_key=True)
data_date = db.Column(db.Date)
details_main = db.Column(db.String(50))
#property
def details_desc(self):
result = object_session(self).\
scalar(
select([Details.details_desc]).
where(Details.details_main == self.details_main)
)
return result
Now, I would like to run query using filter which depends on defined property. I get empty results (of course proper data is in DB). It doesn't work because, probably, I have to map this property. The question is how to do this? (One limitation: FK are not allowed).
Data.query\
.filter(Data.details_desc == unicode('test'))\
.all()
You can implement this with a regular relationship and an association proxy:
class Data(db.Model):
data_id = db.Column(db.Integer, primary_key=True)
data_date = db.Column(db.Date)
details_main = db.Column(db.String(50))
details = relationship(
Details,
primaryjoin=remote(Details.details_main) == foreign(details_main))
details_desc = association_proxy('details', 'details_desc')
Since there are no foreign keys in the schema, you need to tell SQLAlchemy yourself what the join condition for the relationship should be. This is what the remote() and foreign() annotations do.
With that in place, you can use an association_proxy "across" the relationship to create a property on Data which will work the way you want.

How can I use autoincrementing column value as default value for another not nullable column?

Lets say that we have UserModel with following columns:
class UserModel(declarative_base(bind=engine)):
__tablename__ = "users"
id = Column(Integer, autoincrement=True, primary_key=True)
nickname = Column(String, unique=True, nullable=False)
Is there a way to set id as default value for nickname without changing anything outside the model class?
I tried using default=id and server_default=id, but IntegrityError is always raised on commit.
Also, I know that there is no id until commit or flush is performed, but calling flush outside is not an option for me.
Thanks.
You should use hybrid_property for this. Here example:
class UserModel(Base):
__tablename__ = "users"
id = Column(Integer, autoincrement=True, primary_key=True)
nickname_str = Column(String, unique=True)
#hybrid_property
def nickname(self):
return self.nickname_str or str(self.id)
#nickname.expression
def nickname(self):
return case(
[(self.nickname_str != None, self.nickname_str)],
else_=cast(self.id, String)
).label('nickname')
#nickname.setter
def nickname(self, value):
self.nickname_str = value
Full example here.

How can I make property comparison able to be compiled to SQL expression in SQLAlchemy?

There are two tables that one column of table A is pointing another table B's primary key.
But they are placed in different database, so I cannot configure them with foreign key.
Configuring via relationship() is unavailable, so I implemented property attribute manually.
class User(Base):
__tablename__ = 'users'
id = Column(BigInteger, id_seq, primary=True)
name = Column(Unicode(256))
class Article(Base):
__tablename__ = 'articles'
__bind_key__ = 'another_engine'
# I am using custom session configures bind
# each mappers to multiple database engines via this attribute.
id = Column(BigInteger, id_seq, primary=True)
author_id = Column(BigInteger, nullable=False, index=True)
body = Column(UnicodeText, nullable=False)
#property
def author(self):
_session = object_session(self)
return _session.query(User).get(self.author_id)
#author.setter
def author(self, user):
if not isinstance(user, User):
raise TypeError('user must be a instance of User')
self.author_id = user.id
This code works well for simple operations. But it causes dirty queries making SQLAlchemy's features meaningless.
Code would be simple if it was configured via relationship() (e.g. query.filter(author=me)) got messed up(e.g. query.filter(author_id=me.id)).
Relationship(e.g. join) related features are never able to be used in query building.
Can I use property attribute, at least, in building query criterion(filter()/filter_by())?
you can still use relationship here. If you stick to "lazy loading", it will query for the related item in database B after loading the lead item in database A. You can place a ForeignKey() directive in the Column, even if there isn't a real one in the database. Or you can use primaryjoin directly:
class User(Base):
__tablename__ = 'users'
id = Column(BigInteger, id_seq, primary=True)
name = Column(Unicode(256))
class Article(Base):
__tablename__ = 'articles'
__bind_key__ = 'another_engine'
id = Column(BigInteger, id_seq, primary=True)
author_id = Column(BigInteger, nullable=False, index=True)
body = Column(UnicodeText, nullable=False)
author = relationship("User",
primaryjoin="foreign(Article.author_id) == User.id")

python sqlalchemy - map a table to a certain data structure

I have a table defined with relationships and I noticed that even though I don't use joins in my query, the information is still retrieved:
class Employee(Base):
__tablename__ = "t_employee"
id = Column(Identifier(20), Sequence('%s_id_seq' % __tablename__), primary_key=True, nullable=False)
jobs = relationship("EmployeeJob")
roles = relationship("EmployeeRole")
class EmployeeJob(Base):
__tablename__ = "t_employee_job"
id = Column(Integer(20), Sequence('%s_id_seq' % __tablename__), primary_key=True, nullable=False)
employee_id = Column(Integer(20), ForeignKey('t_employee.id', ondelete="CASCADE"), primary_key=True)
job_id = Column(Integer(20), ForeignKey('t_job.id', ondelete="CASCADE"), primary_key=True)
class EmployeeRole(Base):
__tablename__ = "t_employee_role"
id = Column(Integer(20), Sequence('%s_id_seq' % __tablename__), primary_key=True, nullable=False)
employee_id = Column(Integer(20), ForeignKey('t_employee.id', ondelete="CASCADE"), nullable=False)
location_id = Column(Identifier(20), ForeignKey('t_location.id', ondelete="CASCADE"))
role_id = Column(Integer(20), ForeignKey('t_role.id', ondelete="CASCADE"), nullable=False)
session.query(Employee).all() retrieves also the roles and jobs but does so by querying the db for each row.
I have 2 questions about this situation:
1. In terms of performance I guess I should do the join by myself. Am I correct?
2. How do I map a table to a certain data structure? For example, I want to get the list of employees with their roles where each role should be represented by an Array of location ID and role ID e.g. {id:1, jobs:[1,2,3], roles:[[1,1],[1,2],[2,3]]}
1) Please read Eager Loading from the SA documentation.
By default, relationships are loaded lazy on first access to it. In your case, you could use, for example, Joined Load, so that the related rows would be loaded in the same query:
qry = (session.query(Employee).
options(joinedload(Employee.jobs)).
options(joinedload(Employee.roles))
).all()
If you want those relationships to be always loaded when an Employee is loaded, you can configure the relationship to automatically be loaded:
class Employee(Base):
# ...
jobs = relationship("EmployeeJob", lazy="joined")
roles = relationship("EmployeeRole", lazy="subquery")
2) Just create a method to extract the data structure from your query. Something like below should do it (using qry from first part of the answer):
def get_structure(qry):
res = [{"id": e.id,
"jobs": [j.id for j in e.jobs],
"roles": [[r.location_id, r.role_id] for r in e.roles],
}
for e in qry
]
return res
Also note: your EmployeeJob table has funny primary_key, which includes both the id column as well as two ForeignKey columns. I think you should choose either one or the other.
I have finally found a way to accomplish my second issue and decided to answer my own question for the benefit of others:
from sqlalchemy.ext.hybrid import hybrid_property
class Employee(Base):
__tablename__ = "t_employee"
id = Column(Identifier(20), Sequence('%s_id_seq' % __tablename__), primary_key=True, nullable=False)
_jobs = relationship("EmployeeJob", lazy="joined", cascade="all, delete, delete-orphan")
_roles = relationship("EmployeeRole", lazy="joined", cascade="all, delete, delete-orphan")
#hybrid_property
def jobs(self):
return [item.employee_id for item in self._jobs]
#jobs.setter
def jobs(self, value):
self._jobs = [EmployeeJob(job_id=id) for id in value]
#hybrid_property
def roles(self):
return [[item.location_id, item.role_id] for item in self._roles]
#roles.setter
def roles(self, value):
self._roles = [EmployeeRole(location_id=l_id, role_id=r_id) for l_id, r_id in value]
The cascade in the relationship is to ensure that the orphans are deleted once the list is updated, and the decorators define the getter and setter of each complex property
Thank you van for pointing me to the right direction!

Combining relationships

I have a model Zone, a model Entity and a model Transit. Transit is minimally defined as:
class Entity(db.Model):
__tablename__ = 'entities'
id = db.Column(db.Integer, primary_key=True)
contained_by = db.Column(db.Integer, db.ForeignKey('entities.id'))
contains = db.relationship('Entity', backref='container')
discriminator = db.Column('type', db.String(50))
__mapper_args__ = {'polymorphic_on': discriminator}
class Zone(Entity):
__mapper_args__ = {'polymorphic_identity': 'zones'}
routes = db.relationship('Transit')
(stuff goes here)
class Transit(db.Model):
__tablename__ = "transits"
start = db.Column(db.Integer, db.ForeignKey('zones.id'))
end = db.Column(db.Integer, db.ForeignKey('zones.id'))
Zone also has a couple of bits about distance and how defensible it is, but that is irrelevant for this.
First off, due to the fact that Zone is subclassed from Entity using single-table inheritance can I reference zones.id?
Secondly, will the Zone.routes property merge Transit.start and Transit.end?
no, you need to use the table name, which in your case (Single-table inheritance) is entities
no, these will not be merged. You can create two relationships, and have a (hybrid) property which would combine both, but this will only be for reading purposes, as when you would like to modify this property (for example, add Transits), you would still need to specify both sides (start and end).
I am not sure I understand the question here
update-1: as requested in comment, Concrete-Table inheritance code below:
class Zone(Entity):
__mapper_args__ = {'polymorphic_identity': 'zones'}
__tablename__ = "zones"
id = Column(Integer, ForeignKey('entities.id'), primary_key=True)
#property
def transits(self):
return self.transits_from_here + self.transits_to_here
class Transit(Base):
__tablename__ = "transits"
id = Column(Integer, primary_key=True)
start = Column(Integer, ForeignKey('zones.id'))
end = Column(Integer, ForeignKey('zones.id'))
s_zone = relationship(Zone, primaryjoin=(start==Zone.id), backref="transits_from_here")
e_zone = relationship(Zone, primaryjoin=(end==Zone.id), backref="transits_to_here")

Categories