How to query those Objects that are not joined (SQLAlchemy) - python

I have two models in SQLAlchemy, having a many-to-many relationship
team_user_table = Table('team_user', Base.metadata,
Column('user_id', Integer, ForeignKey('users.id')),
Column('team_id', Integer, ForeignKey('teams.id'))
)
class User(Base):
""" The SQLAlchemy declarative model class for a User object. """
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String, unique=True)
class Team(Base):
""" The SQLAlchemy declarative model class for a Team object. """
__tablename__ = 'teams'
id = Column(Integer, primary_key=True)
name = Column(Text, unique=True)
members = relationship("User",
secondary=team_user_table,
backref="memberteams")
I would like to query those users that are not member of a specific team.
In SQL (for example):
SELECT u.id,u.name FROM users u WHERE u.id NOT IN (SELECT tu.user_id FROM team_user tu WHERE tu.team_id=?);
How can I do this in SQLAlchemy?

This does what you want:
team_id = 1
query = session.query(User.id).filter(~User.memberteams.any(Team.id == team_id))
This is the SQL it outputs (on MySQL):
SELECT users.id AS users_id, users.name AS users_name
FROM users
WHERE NOT (EXISTS (SELECT 1
FROM team_user, teams
WHERE users.id = team_user.user_id AND teams.id = team_user.team_id AND teams.id = %s))
This is not exactly what your query looks like now, but I think this is the way it should be done. Check the docs for this.
I tested this on MySQL. However, I think it should work on any other.
To have the exact query you are looking for, you might need to look into using subqueries in combination with the filter statement. I think you will need explicit reference to the team_user table in your code to make it work.

I believe this should work and it does not use subqueries.
session.query(User).join(team_users_table).filter(team_users_table.team_id != OTHER_TEAM)

Related

sqlAlchemy: Could not determine join condition between parent/child tables

I've been trying to solve this for hours now, I've read through a few stack overflows, the official SQL alchemy documentation and watched youtube tutorials and it's all the same code as mine.
I have 3 tables - Users and Roles, and user_role which joins them.
I keep getting the following error:
sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship
Users.role - there are no foreign keys linking these tables via secondary table 'user_role'. Ensure that referencing
columns are associated with a ForeignKey or ForeignKeyConstraint, or specify 'primaryjoin' and 'secondaryjoin'
expressions.
My code:
engine = create_engine('***',
connect_args={'options': '-csearch_path={}'.format('flaskProject')})
Base = declarative_base(engine)
Base.query = db.session.query_property()
user_role = Table('user_role', Base.metadata,
Column('userId', Integer, ForeignKey("Users.id")),
Column('roleId', Integer, ForeignKey("Roles.id")))
class Users(UserMixin, Base):
__tablename__ = 'users'
__table_args__ = {'schema': 'flaskProject'}
id = Column(Integer, primary_key=True)
username = Column(String)
email = Column(String)
password_hash = Column(String)
role = relationship("Roles", secondary = user_role)
class Roles(Base):
__table_args__ = {'schema': 'flaskProject'}
__tablename__ = 'roles'
id = Column(Integer, primary_key=True)
name = Column(String)
I found out the issues with my secondary table - I was missing the schema in the table definition, had to add the schema to the foreign keys, and column names are case-sensitive
user_role = Table('user_role', Base.metadata,
Column('userid', Integer, ForeignKey('flaskProject.users.id')),
Column('roleid', Integer, ForeignKey('flaskProject.roles.id'))
,schema='flaskProject')

SQLAlchemy group_concat and duplicates

When I try to join a many-to-many table and group it by the main-id I am getting duplicates when I add the second many-to-many table.
Here is how my models look like:
Models
user
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
user_fistName = db.Column(db.String(64))
...
student_identifier
student_identifier = db.Table('student_identifier',
db.Column('class_id', db.Integer, db.ForeignKey('class.class_id')),
db.Column('id', db.Integer, db.ForeignKey('user.id'))
)
class
class Class(db.Model):
sqlite_autoincrement=True
class_id = db.Column(db.Integer, primary_key=True)
class_name = db.Column(db.String(128), unique=True)
mm_children = db.relationship('User', secondary=student_identifier, backref=db.backref('classes'))
class_course_identifier
class_course_identifier = db.Table('class_course_identifier',
db.Column('course_id', db.Integer, db.ForeignKey('course.course_id')),
db.Column('class_id', db.Integer, db.ForeignKey('class.class_id'))
)
database structure
Well I am using SQLAlchemy to select the desired tables with the data I want. with this session.query
db.session.query(
Class.class_id,
Class.class_name,
func.group_concat(User.user_fistName),
func.group_concat(Course.course_name)
).filter(Class.courses, User.classes).group_by(Class.class_id)
the problem is that I am getting duplicates of both the courses AND names, so if the course has two users it will print the students and the course two times.
Here is how it is looking:
wrong view
And here is how it should look:
correct view
the problem
the problem is coming when I am adding the second many-to-many table, for example users/student-identifier. If I remove the line where I "join" it, I am getting the duplicates. Is there anyway to correct this? Or should I use RAW-SQL instead(and if yes, how?)
Found out the solution, and it is quite simple.
RAW SQL
SELECT
class.class_id,
class.class_name,
GROUP_CONCAT(DISTINCT course.course_name),
GROUP_CONCAT(DISTINCT user.user_fistName)
FROM
class
JOIN class_course_identifier ON class.class_id = class_course_identifier.class_id
JOIN course ON class_course_identifier.course_id = course.course_id
JOIN student_identifier ON class.class_id = student_identifier.class_id
JOIN user ON student_identifier.id = user.id
GROUP BY class.class_id
SQLAlchemy
db.session.query(
Class.class_id,
Class.class_name,
func.group_concat(User.user_fistName.distinct()),
func.group_concat(Course.course_name.distinct())
).filter(Class.courses, User.classes).group_by(Class.class_id)
Simply add the distinct() to the desired column you want to be unique

Many-to-many with flask and sqlalchemy in python

I am trying to do a many-to-many relationship table with more information than just the two ids. It doesnt work. I obviously standardised my table with generic names. But the use case could be like a user table , with a post table and a likes relationship table, something like that. when i do these table, python gave me that :
sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Table2.rela1 - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
So here is my relationship table
class Relationship(db.Model):
id = db.Column(db.Integer, primary_key = True)
table1_id = db.Column(db.Integer, db.ForeignKey('Table1.id')),
table2_id = db.Column(db.Integer, db.ForeignKey('Table2.id')),
value = db.Column(db.Integer), #a number
date = db.Column(db.DateTime) #actual time of the entry
here is my table1
class Table1(db.Model):
id = db.Column(db.Integer, primary_key = True)
rela1 = db.relationship('Relationship', backref = 'rel1', lazy = 'dynamic')
rela2 = db.relationship('Table2', backref = 'rel2', lazy = 'dynamic')
here is my table 2
class Table2(db.Model):
id = db.Column(db.Integer, primary_key = True)
table1_id = db.Column(db.Integer, db.ForeignKey('table1.id'))
rela1 = db.relationship('Relationship', backref = 'rel3', lazy = 'dynamic')
Thanks for helping me.
If that can be solved then my second issue is creating a function to compute the total 'value' per Table2 object, regardless of the which table1 object post the value. something like a select with sum and group by table2.id , but i dont really understand how to do it with python and flask and sqlalchemy.
Thanks.
EDIT1
Using http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html#association-object
i changed to
class Relationship(db.Model):
__tablename__ = 'relationship'
table1_id = db.Column(db.Integer, db.ForeignKey('Table1.id'), primary_key = True),
table2_id = db.Column(db.Integer, db.ForeignKey('Table2.id'), primary_key = True),
value = db.Column(db.Integer), #a number
date = db.Column(db.DateTime) #actual time of the entry
table2obj = db.relationship("Table2", backref="table2_assocs")
then
class Table2(db.Model):
__tablename__ = 'table2'
id = db.Column(db.Integer, primary_key = True)
table1_id = db.Column(db.Integer, db.ForeignKey('table1.id'))
and table 1 is unchanged except the addition of tablename
but now i get
sqlalchemy.exc.ArgumentError: Mapper Mapper|Relationship|relatioship could not assemble any primary key columns for mapped table 'Relationship'
there are two relationship declarations on Table1, but from the code you've posted, I can see only one path to Table2, through Relationship.table2_id.
It looks like you are trying to have a many-to-many relationship with extra metadata on the intermediate relation. This is called the association object pattern in the sqlalchemy docs. For that, everything you've got so far, except the Table1.rela2 is good, you pretty well have it. That "rogue" relationship needs to be removed.
to get the convenience of having a real relationship from Table1 to Table2, you need to use an association proxy.

sqlalchemy: referencing label()'d column in a filter or clauselement

I'm trying to perform a query that works across a many->many relation ship between bmarks and tags with a secondary table of bmarks_tags. The query involves several subqueries and I have a need to DISTINCT a column. I later want to join that to another table via the DISTINCT'd ids.
I've tried it a few ways and this seems closest:
tagid = alias(Tag.tid.distinct())
test = select([bmarks_tags.c.bmark_id],
from_obj=[bmarks_tags.join(DBSession.query(tagid.label('tagid'))),
bmarks_tags.c.tag_id == tagid])
return DBSession.execute(qry)
But I get an error:
⇝ AttributeError: '_UnaryExpression' object has no attribute 'named_with_column'
Does anyone know how I can perform the join across the bmarks_tags.tag_id and the result of the Tag.tid.distinct()?
Thanks
Schema:
# this is the secondary table that ties bmarks to tags
bmarks_tags = Table('bmark_tags', Base.metadata,
Column('bmark_id', Integer, ForeignKey('bmarks.bid'), primary_key=True),
Column('tag_id', Integer, ForeignKey('tags.tid'), primary_key=True)
)
class Tag(Base):
"""Bookmarks can have many many tags"""
__tablename__ = "tags"
tid = Column(Integer, autoincrement=True, primary_key=True)
name = Column(Unicode(255), unique=True)
Something like this should work:
t = DBSession.query(Tag.tid.distinct().label('tid')).subquery('t')
test = select([bmarks_tags.c.bmark_id], bmarks_tags.c.tag_id == t.c.tid)
return DBSession.execute(test)
It is hard to tell what you are trying to accomplish, but since you are using orm anyways (and there is not much reason anymore to go with bare selects in sa these days), you should probably start by establishing a many-to-many relation:
bmarks_tags = Table('bmark_tags', Base.metadata,
Column('bmark_id', Integer, ForeignKey('bmarks.bid'), primary_key=True),
Column('tag_id', Integer, ForeignKey('tags.tid'), primary_key=True)
)
class Tag(Base):
"""Bookmarks can have many many tags"""
__tablename__ = "tags"
tid = Column(Integer, primary_key=True)
name = Column(Unicode(255), unique=True)
class BMark(Base):
__tablename__ = 'bmarks'
bid = Column(Integer, primary_key=True)
tags = relation(Tag, secondary=bmarks_tags, backref="bmarks")
Then get your query and go from there:
query = DBSession.query(BMark).join(BMark.tags)
If not, give us the actual sql you are trying to make sqlalchemy emit.

SqlAlchemy Select Relation Type

I have a simple One-to-Many relation mapped with SqlAlchemy:
Base = declarative_base()
class Type(Base):
__tablename__ = "entity_types"
type = Column(String(100), primary_key=True)
description = Column(String(300))
class Entity(Base):
__tablename__ = "entities"
id = Column(Integer, primary_key=True)
type_id = Column('type', String(100), ForeignKey(Types.type),
nullable=False)
type = relation(Type, backref='entities')
value = Column(Text, nullable=False)
I want to query all types ever used in an entity. In pure SQL I would accomplish this by:
SELECT entity_types.*
FROM entities
JOIN entity_types ON entities.type == entity_types.type
GROUP BY entity_types.type
How do I solve this using SqlAlchemy's ORM-Engine?
I've tried these queries, but they all don't return what I want:
session.query(Action.type).group_by(Action.type).all()
session.query(Type).select_from(Action).group_by(Type).all()
I've also tried using options(joinedload('type')), but I found out, this is only used to force eager loading and to bypass lazy-loading.
ADDITION: I've just added the backref in the relation of Entity. I think the problem is solvable by querying count(Type.entities) > 0, but I cannot figure out how to exactly form a valid ORM query.
I've just figured it out:
session.query(ActionType).filter(ActionType.actions.any()).all()
The any() does the trick.

Categories

Resources