[sqlalchemy] How to have SQL IF in sqlalchemy

2014-06-18 Thread Vineet Goel
Hi,

I am trying to convert the following SQL to SQLAlchemy:

SELECT teams.department, teams.team,
IF(employee_managers.team_id IS NOT NULL, employee_managers.manager, 
teams.default_manager) AS manager
FROM teams
LEFT JOIN employee_managers ON employee_managers.team_id = teams.id
WHERE teams.department = 'hr' and (employee_managers.name = 'vineet' OR 
employee_managers.name IS NULL)

where my models look like this:

class EmployeeTeam(db.Model):
  __tablename__ = 'teams'
  id = db.Column(db.Integer, primary_key=True)
  name = db.Column(db.String(140), nullable=False)
  department = db.Column(db.String(140), nullable=False)
  team = db.Column(db.String(140), nullable=False)
  default_manager = db.Column(db.String(140), nullable=False)
class EmployeeManagers(db.Model):
  __tablename__ = 'employee_managers'
  id = db.Column(db.Integer, primary_key=True)
  name = db.Column(db.String(140), nullable=False)
  team_id = db.Column(db.Integer, db.ForeignKey('teams.id'))
  team = db.relationship('EmployeeTeam')
  manager = db.Column(db.String(140), nullable=False)

After hours of googling, I found some func.IF kind of stuff but they all 
keep giving me an  OperationalError: (OperationalError) (1248, 'Every 
derived table must have its own alias').

Does anyone know a simple way to convert this SQL to SQLAlchemy?

Any help would be much appreciated.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] How to have SQL IF in sqlalchemy

2014-06-18 Thread Mike Bayer

On 6/18/14, 8:31 PM, Vineet Goel wrote:
 Hi,

 I am trying to convert the following SQL to SQLAlchemy:

 |SELECT teams.department, teams.team,
 IF(employee_managers.team_id IS NOT NULL, employee_managers.manager, 
 teams.default_manager) AS manager
 FROM teams
 LEFT JOIN employee_managers ON employee_managers.team_id = teams.id
 WHERE teams.department = 'hr' and (employee_managers.name = 'vineet' OR 
 employee_managers.name IS NULL)|

the SQL standard version of IF is CASE, SQLAlchemy supports CASE
directly see
http://docs.sqlalchemy.org/en/rel_0_9/core/sqlelement.html?highlight=case#sqlalchemy.sql.expression.case



 where my models look like this:

 |class EmployeeTeam(db.Model):
   __tablename__ = 'teams'
   id = db.Column(db.Integer, primary_key=True)
   name = db.Column(db.String(140), nullable=False)
   department = db.Column(db.String(140), nullable=False)
   team = db.Column(db.String(140), nullable=False)
   default_manager = db.Column(db.String(140), nullable=False)

 class EmployeeManagers(db.Model):
   __tablename__ = 'employee_managers'
   id = db.Column(db.Integer, primary_key=True)
   name = db.Column(db.String(140), nullable=False)
   team_id = db.Column(db.Integer, db.ForeignKey('teams.id'))
   team = db.relationship('EmployeeTeam')
   manager = db.Column(db.String(140), nullable=False)|
 After hours of googling, I found some func.IF kind of stuff but they
 all keep giving me an  OperationalError: (OperationalError) (1248,
 'Every derived table must have its own alias').

 Does anyone know a simple way to convert this SQL to SQLAlchemy?

 Any help would be much appreciated.
 -- 
 You received this message because you are subscribed to the Google
 Groups sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send
 an email to sqlalchemy+unsubscr...@googlegroups.com
 mailto:sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com
 mailto:sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.