Express SQL Join and Many-to-Many filter in SQLAlchemy format [duplicate]












0
















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.










share|improve this question













marked as duplicate by Ilja Everilä python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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


















0
















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.










share|improve this question













marked as duplicate by Ilja Everilä python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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
















0












0








0









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.










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 9:15









Attack68Attack68

1,1381412




1,1381412




marked as duplicate by Ilja Everilä python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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ä python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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





















  • 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














1 Answer
1






active

oldest

votes


















0














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()





share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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()





    share|improve this answer




























      0














      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()





      share|improve this answer


























        0












        0








        0







        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()





        share|improve this answer













        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()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 10:37









        Florian HFlorian H

        1,2321615




        1,2321615

















            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine