title: Where clause in SQLAlchemy
body: <p>I have a question about SQLAlchemy.</p>
  
  <p>Currently I have two models using Flask - SQLAlchemy :</p>
  
  <pre><code>class Action(db.Model):
  
      __tablename__ = 'ACTION'
  
      _FIELDS_LABELS = ("ID", "MODULE_ID", "CATEGORY", "LABEL", "COMMAND", "ARGS", "FILE_ID")
      _REPR_FIELDS = ("MODULE_NAME", "FILE_NAME")
  
      id = db.Column(_FIELDS_LABELS[0], db.Integer, primary_key=True)
      module_id = db.Column(_FIELDS_LABELS[1], db.Integer, db.ForeignKey('MODULE.ID'))
      category = db.Column(_FIELDS_LABELS[2], db.String())
      label = db.Column(_FIELDS_LABELS[3], db.String())
      command = db.Column(_FIELDS_LABELS[4], db.String())
      args = db.Column(_FIELDS_LABELS[5], db.String())
      file_id = db.Column(_FIELDS_LABELS[6], db.Integer(), db.ForeignKey('FILE.ID'))
  
      file = db.relationship('File')
  </code></pre>
  
  <p>and </p>
  
  <pre><code>class Module(db.Model):
  
      __tablename__ = 'MODULE'
  
      _FIELDS_LABELS = ("ID", "NAME")
  
      id = db.Column(_FIELDS_LABELS[0], db.Integer,  primary_key=True)
      name = db.Column(_FIELDS_LABELS[1], db.String(), unique=True)
      actions = db.relationship('Action')
  </code></pre>
  
  <p>With Flask I try to get actions with URL like that : </p>
  
  <pre><code>http://ip:port/modules/0/actions/18
  </code></pre>
  
  <p>So I have an action blueprint :</p>
  
  <pre><code>action_blueprint = Blueprint('action',
                               __name__,
                               url_prefix="/modules/&lt;string:module_id&gt;/actions")
  </code></pre>
  
  <p>And route : </p>
  
  <pre><code>@action_blueprint.route('/', methods=['GET'])
  def read_all(module_id):
  
      if module_id == '*':
          if len(request.args) == 0:
              actions = Action.query.all()
              return jsonify(list_to_json(actions))
          else:
              try:
                  action = Action.query.filter_by(**request.args.to_dict()).first()
                  return jsonify(action.serialize)
              except InvalidRequestError as i:
                  return json_response(400, "InvalidRequestError : {}".format(i.args))
      else:
          try:
              module = Module.query.get(module_id)
          except AttributeError as a:
              return json_response(400, "'{}' needed to be '*' or a number.".format(module_id))
  
          if len(request.args) == 0:
              return jsonify(actions=list_to_json(module.actions))
  
          action = Action.query.join(Module).filter(Module.id == module_id).filter_by(**request.args.to_dict()).first()
          return jsonify(action.serialize)
  </code></pre>
  
  <p>But I have the following error :</p>
  
  <pre><code>AttributeError: 'NoneType' object has no attribute 'serialize'
  </code></pre>
  
  <p>The generated query : </p>
  
  <pre><code>SELECT "ACTION"."ID" AS "ACTION_ID", "ACTION"."MODULE_ID" AS "ACTION_MODULE_ID", "ACTION"."CATEGORY" AS "ACTION_CATEGORY", "ACTION"."LABEL" AS "ACTION_LABEL", "ACTION"."COMMAND" AS "ACTION_COMMAND", "ACTION"."ARGS" AS "ACTION_ARGS", "ACTION"."FILE_ID" AS "ACTION_FILE_ID"
  FROM "ACTION" JOIN "MODULE" ON "MODULE"."ID" = "ACTION"."MODULE_ID"
  WHERE "MODULE"."ID" = ? AND "MODULE"."ID" = ?
  </code></pre>
  
  <p>There is a probleme with the end of the query, but I don't really know if </p>
  
  <pre><code>action = Action.query.join(Module).filter(Module.id == module_id).filter_by(**request.args.to_dict()).first()
  </code></pre>
  
  <p>is correct or not.</p>