SQLalchemy primaryjoin attribute not working in Python 3.4 (worked using Python 2.7)
<p>I recently tried upgrading our code from Python 2.7 to Python 3.4.</p>

<p>In Python 2.7 this simply worked:</p>

<pre><code>class Maatje(CommonColumns):
    koppelvoorstellen = db.relationship("Koppelvoorstel",
                        primaryjoin="or_(Maatje.id == Koppelvoorstel.deelnemerID," \
                        + "Maatje.id == Koppelvoorstel.vrijwilligerID)",
                        cascade="all,delete-orphan")
</code></pre>

<p>But using Python 3.4 (and mod_wsgi 4.4 as compiled from source against Python 3.4) I get an error when I try to query Maatje, in my Apache 2.4 error log:</p>

<pre><code>[wsgi:error] [pid 6169] sqlalchemy.exc.ProgrammingError:      
(mysql.connector.errors.ProgrammingError) 1064 (42000): You have an     
error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near '%(param_1)s = 
koppelvoorstel.`vrijwilligerID`' at line 3 [SQL: 'SELECT 
koppelvoorstel.id AS koppelvoorstel_id, koppelvoorstel.aangemaakt AS     
koppelvoorstel_aangemaakt, koppelvoorstel.gewijzigd AS 
koppelvoorstel_gewijzigd, koppelvoorstel.etag AS koppelvoorstel_etag, 
koppelvoorstel.`deelnemerID` AS `koppelvoorstel_deelnemerID`,    
koppelvoorstel.`vrijwilligerID` AS `koppelvoorstel_vrijwilligerID`, 
koppelvoorstel.akkoordvrijwilliger AS 
koppelvoorstel_akkoordvrijwilliger, koppelvoorstel.akkoorddeelnemer AS 
koppelvoorstel_akkoorddeelnemer, 
koppelvoorstel.redenafwijzingvrijwilliger AS 
koppelvoorstel_redenafwijzingvrijwilliger, 
koppelvoorstel.redenafwijzingdeelnemer AS 
koppelvoorstel_redenafwijzingdeelnemer, koppelvoorstel.stopdatum AS 
koppelvoorstel_stopdatum, koppelvoorstel.redenstoppenvrijwilliger AS 
koppelvoorstel_redenstoppenvrijwilliger, 
koppelvoorstel.redenstoppendeelnemer AS 
koppelvoorstel_redenstoppendeelnemer \\nFROM koppelvoorstel \\nWHERE %
(param_1)s = koppelvoorstel.`deelnemerID` OR %(param_1)s = 
koppelvoorstel.`vrijwilligerID`'] [parameters: {'param_1': 1}]
</code></pre>

<p>Looks like perfectly valid SQL to me (I am assuming \\n will become a newline though). I tried the SQL-query using phpMyAdmin (without the \n's) and with the parameter filled in (a one) and it understood the query and came up with valid results.</p>

<p>I've tried writing the <code>primaryjoin</code> in other forms, such as without string, with or_ and with "|" as or-operator. Also tried the <code>in_</code>-operator.</p>

<p>I'm using Ubuntu 14.04 with mod_wsgi 4.4, SQLalchemy 1.0.12 and Flask-SQLalchemy 2.1.</p>

<p>I'm using <code>python3-mysql.connector</code> (Ubuntu package for MySQL Connector/Python).</p>

<p>If I delete one of the equality comparisons (does not matter which one) and not using the <code>or_</code>-operator then it does not throw an error. But I need both using the <code>or_</code>-operator.</p>

<p><strong>6 March 2016:</strong>
Now I've isolated the problem a bit. Running the following code with python2.7 poses no problems:</p>

<pre><code>from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://username:password@127.0.0.1/primaryjoindb'
db = SQLAlchemy(app)

class Coupleproposal(db.Model):
    __tablename__ = 'coupleproposal'
    id = db.Column(db.Integer, primary_key=True)
    participantID = db.Column(db.Integer, db.ForeignKey('buddy.id'), nullable = False)
    volunteerID = db.Column(db.Integer, db.ForeignKey('buddy.id'), nullable = False)

    participant = db.relationship("Buddy", foreign_keys=[participantID])
    volunteer = db.relationship("Buddy", foreign_keys=[volunteerID])
    __table_args__ = (db.UniqueConstraint('participantID', 'volunteerID', name='_couple_uc'),)

class Buddy(db.Model):
    __tablename__ = 'buddy'
    id = db.Column(db.Integer, primary_key=True)
    coupleproposals = db.relationship("Coupleproposal", \
                    primaryjoin="(Buddy.id == Coupleproposal.participantID) | (Buddy.id == Coupleproposal.volunteerID)", \
                    cascade="all,delete-orphan")

db.create_all()
b1 = Buddy()
b2 = Buddy()
db.session.add(b1)
db.session.add(b2)
cp1 = Coupleproposal(participantID=1, volunteerID=1)
db.session.add(cp1)
db.session.commit()
for aBuddy in db.session.query(Buddy).all():
    print(aBuddy.id)
    print(aBuddy.coupleproposals)
</code></pre>

<p>But running the code with python3.4 throws this error:</p>

<blockquote>
  <p>sqlalchemy.exc.ProgrammingError: (mysql.connector.errors.ProgrammingError) 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%(param_1)s = coupleproposal.<code>volunteerID</code>' at line 3 [SQL: 'SELECT coupleproposal.id AS coupleproposal_id, coupleproposal.<code>participantID</code> AS <code>coupleproposal_participantID</code>, coupleproposal.<code>volunteerID</code> AS <code>coupleproposal_volunteerID</code> \nFROM coupleproposal \nWHERE %(param_1)s = coupleproposal.<code>participantID</code> OR %(param_1)s = coupleproposal.<code>volunteerID</code>'] [parameters: {'param_1': 1}]</p>
</blockquote>