Express SQL Join and Many-to-Many filter in SQLAlchemy format [duplicate]
This question already has an answer here:
SQLAlchemy: filter by membership in at least one many-to-many related table
3 answers
How to query Many-To-Many SQLAlchemy
1 answer
How to query many-to-many based on some constraints in flask sqlalchemy?
1 answer
Im trying to learn SQLAlchemy best practice for a) doing what I need and b) doing it in the most efficient way.
I have currently have 3 tables, defining a many-to-many relationship between club
and person
.
class Club(db.Model)
__tablename__ = 'club'
id = db.Column(db.Integer, primary_key=True)
type = db.Column(db.String)
class Person(db.Model)
__tablename__ = 'person'
id = db.Column(db.Integer, primary_key=True)
club_person_assoc = Table('club_person_ass', db.Model.metadata,
Column('club_id', db.Integer, db.ForeignKey('club.id')),
Column('person_id', db.Integer, db.ForeignKey('person.id'))
)
What is a good way of returning: all clubs
that a particular person
(person.id==1)
is a member of, conditioned on a type of club (club.type='sports')
In SQL I can express the statement directly as:
SELECT *
FROM (
SELECT *
FROM club
WHERE club.type = 'sports'
) AS lef
INNER JOIN (
SELECT *
FROM club_person_assoc
WHERE club_person_assoc.person_id = 1
) AS rig
ON lef.id = rig.club_id;
I can fudge it using python list comprehension by doing:
class Person(db.Model)
...
clubs = db.relationship('Club', secondary=club_person_assoc)
# later using list comprehension
filtered = [club for club in person_x.clubs if club.type == 'sports']
but the above of course feels contrived and slower than a native DB query.
If you have any recommendations for good locations to learn this stuff besides the docs, in a more tutorial style I would appreciate the comment.
python sqlalchemy many-to-many flask-sqlalchemy
marked as duplicate by Ilja Everilä
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 11:07
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
SQLAlchemy: filter by membership in at least one many-to-many related table
3 answers
How to query Many-To-Many SQLAlchemy
1 answer
How to query many-to-many based on some constraints in flask sqlalchemy?
1 answer
Im trying to learn SQLAlchemy best practice for a) doing what I need and b) doing it in the most efficient way.
I have currently have 3 tables, defining a many-to-many relationship between club
and person
.
class Club(db.Model)
__tablename__ = 'club'
id = db.Column(db.Integer, primary_key=True)
type = db.Column(db.String)
class Person(db.Model)
__tablename__ = 'person'
id = db.Column(db.Integer, primary_key=True)
club_person_assoc = Table('club_person_ass', db.Model.metadata,
Column('club_id', db.Integer, db.ForeignKey('club.id')),
Column('person_id', db.Integer, db.ForeignKey('person.id'))
)
What is a good way of returning: all clubs
that a particular person
(person.id==1)
is a member of, conditioned on a type of club (club.type='sports')
In SQL I can express the statement directly as:
SELECT *
FROM (
SELECT *
FROM club
WHERE club.type = 'sports'
) AS lef
INNER JOIN (
SELECT *
FROM club_person_assoc
WHERE club_person_assoc.person_id = 1
) AS rig
ON lef.id = rig.club_id;
I can fudge it using python list comprehension by doing:
class Person(db.Model)
...
clubs = db.relationship('Club', secondary=club_person_assoc)
# later using list comprehension
filtered = [club for club in person_x.clubs if club.type == 'sports']
but the above of course feels contrived and slower than a native DB query.
If you have any recommendations for good locations to learn this stuff besides the docs, in a more tutorial style I would appreciate the comment.
python sqlalchemy many-to-many flask-sqlalchemy
marked as duplicate by Ilja Everilä
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 11:07
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
As to your tutorial request, why not try the official tutorial: docs.sqlalchemy.org/en/latest/orm/tutorial.html#using-exists?
– Ilja Everilä
Nov 16 '18 at 11:06
add a comment |
This question already has an answer here:
SQLAlchemy: filter by membership in at least one many-to-many related table
3 answers
How to query Many-To-Many SQLAlchemy
1 answer
How to query many-to-many based on some constraints in flask sqlalchemy?
1 answer
Im trying to learn SQLAlchemy best practice for a) doing what I need and b) doing it in the most efficient way.
I have currently have 3 tables, defining a many-to-many relationship between club
and person
.
class Club(db.Model)
__tablename__ = 'club'
id = db.Column(db.Integer, primary_key=True)
type = db.Column(db.String)
class Person(db.Model)
__tablename__ = 'person'
id = db.Column(db.Integer, primary_key=True)
club_person_assoc = Table('club_person_ass', db.Model.metadata,
Column('club_id', db.Integer, db.ForeignKey('club.id')),
Column('person_id', db.Integer, db.ForeignKey('person.id'))
)
What is a good way of returning: all clubs
that a particular person
(person.id==1)
is a member of, conditioned on a type of club (club.type='sports')
In SQL I can express the statement directly as:
SELECT *
FROM (
SELECT *
FROM club
WHERE club.type = 'sports'
) AS lef
INNER JOIN (
SELECT *
FROM club_person_assoc
WHERE club_person_assoc.person_id = 1
) AS rig
ON lef.id = rig.club_id;
I can fudge it using python list comprehension by doing:
class Person(db.Model)
...
clubs = db.relationship('Club', secondary=club_person_assoc)
# later using list comprehension
filtered = [club for club in person_x.clubs if club.type == 'sports']
but the above of course feels contrived and slower than a native DB query.
If you have any recommendations for good locations to learn this stuff besides the docs, in a more tutorial style I would appreciate the comment.
python sqlalchemy many-to-many flask-sqlalchemy
This question already has an answer here:
SQLAlchemy: filter by membership in at least one many-to-many related table
3 answers
How to query Many-To-Many SQLAlchemy
1 answer
How to query many-to-many based on some constraints in flask sqlalchemy?
1 answer
Im trying to learn SQLAlchemy best practice for a) doing what I need and b) doing it in the most efficient way.
I have currently have 3 tables, defining a many-to-many relationship between club
and person
.
class Club(db.Model)
__tablename__ = 'club'
id = db.Column(db.Integer, primary_key=True)
type = db.Column(db.String)
class Person(db.Model)
__tablename__ = 'person'
id = db.Column(db.Integer, primary_key=True)
club_person_assoc = Table('club_person_ass', db.Model.metadata,
Column('club_id', db.Integer, db.ForeignKey('club.id')),
Column('person_id', db.Integer, db.ForeignKey('person.id'))
)
What is a good way of returning: all clubs
that a particular person
(person.id==1)
is a member of, conditioned on a type of club (club.type='sports')
In SQL I can express the statement directly as:
SELECT *
FROM (
SELECT *
FROM club
WHERE club.type = 'sports'
) AS lef
INNER JOIN (
SELECT *
FROM club_person_assoc
WHERE club_person_assoc.person_id = 1
) AS rig
ON lef.id = rig.club_id;
I can fudge it using python list comprehension by doing:
class Person(db.Model)
...
clubs = db.relationship('Club', secondary=club_person_assoc)
# later using list comprehension
filtered = [club for club in person_x.clubs if club.type == 'sports']
but the above of course feels contrived and slower than a native DB query.
If you have any recommendations for good locations to learn this stuff besides the docs, in a more tutorial style I would appreciate the comment.
This question already has an answer here:
SQLAlchemy: filter by membership in at least one many-to-many related table
3 answers
How to query Many-To-Many SQLAlchemy
1 answer
How to query many-to-many based on some constraints in flask sqlalchemy?
1 answer
python sqlalchemy many-to-many flask-sqlalchemy
python sqlalchemy many-to-many flask-sqlalchemy
asked Nov 16 '18 at 9:15
Attack68Attack68
1,1381412
1,1381412
marked as duplicate by Ilja Everilä
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 11:07
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Ilja Everilä
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 11:07
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
As to your tutorial request, why not try the official tutorial: docs.sqlalchemy.org/en/latest/orm/tutorial.html#using-exists?
– Ilja Everilä
Nov 16 '18 at 11:06
add a comment |
As to your tutorial request, why not try the official tutorial: docs.sqlalchemy.org/en/latest/orm/tutorial.html#using-exists?
– Ilja Everilä
Nov 16 '18 at 11:06
As to your tutorial request, why not try the official tutorial: docs.sqlalchemy.org/en/latest/orm/tutorial.html#using-exists?
– Ilja Everilä
Nov 16 '18 at 11:06
As to your tutorial request, why not try the official tutorial: docs.sqlalchemy.org/en/latest/orm/tutorial.html#using-exists?
– Ilja Everilä
Nov 16 '18 at 11:06
add a comment |
1 Answer
1
active
oldest
votes
I don't have a tutorial recommendation but doing a list comprehension is the exact opposite of doing it in the most efficient way.
The way of doing this would be something like:
Session().query(Club.Id).join(Person).filter(Club.type == "sports").filter(Person.id == 1).all()
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't have a tutorial recommendation but doing a list comprehension is the exact opposite of doing it in the most efficient way.
The way of doing this would be something like:
Session().query(Club.Id).join(Person).filter(Club.type == "sports").filter(Person.id == 1).all()
add a comment |
I don't have a tutorial recommendation but doing a list comprehension is the exact opposite of doing it in the most efficient way.
The way of doing this would be something like:
Session().query(Club.Id).join(Person).filter(Club.type == "sports").filter(Person.id == 1).all()
add a comment |
I don't have a tutorial recommendation but doing a list comprehension is the exact opposite of doing it in the most efficient way.
The way of doing this would be something like:
Session().query(Club.Id).join(Person).filter(Club.type == "sports").filter(Person.id == 1).all()
I don't have a tutorial recommendation but doing a list comprehension is the exact opposite of doing it in the most efficient way.
The way of doing this would be something like:
Session().query(Club.Id).join(Person).filter(Club.type == "sports").filter(Person.id == 1).all()
answered Nov 16 '18 at 10:37
Florian HFlorian H
1,2321615
1,2321615
add a comment |
add a comment |
As to your tutorial request, why not try the official tutorial: docs.sqlalchemy.org/en/latest/orm/tutorial.html#using-exists?
– Ilja Everilä
Nov 16 '18 at 11:06