def get_query(self, database_session, state):
        query = Query(self.id_column)
        already_joined = []
        for column_key in self.columns:
            if self.column_def[column_key].get("column") is not None:
                column = self.column_def[column_key]["column"]
                if column.class_ != self.model:
                    if self.column_def[column_key]["outer_join"]:
                        if self.column_def[column_key][
                                "join_expression"] is not None:
                            query = query.outerjoin(
                                column.class_,
                                self.column_def[column_key]["join_expression"])
                        else:
                            query = query.outerjoin(column.class_)
                    else:
                        if self.column_def[column_key][
                                "join_expression"] is not None:
                            query = query.join(
                                column.class_,
                                self.column_def[column_key]["join_expression"])
                        else:
                            query = query.join(column.class_)

                query = query.add_column(column.label(column_key))
            elif self.column_def[column_key].get("expression") is not None:
                expression = self.column_def[column_key]["expression"]
                if self.column_def[column_key]["join"] is None:
                    query = query.add_column(expression.label(column_key))
                elif self.column_def[column_key][
                        "join"] is not None and not self.column_def[
                            column_key]["outer_join"]:
                    if not self.column_def[column_key][
                            "join"] in already_joined:
                        if self.column_def[column_key][
                                "join_expression"] is not None:
                            query = query.join(
                                self.column_def[column_key]["join"],
                                self.column_def[column_key]["join_expression"])
                        else:
                            query = query.join(
                                self.column_def[column_key]["join"])

                        already_joined.append(
                            self.column_def[column_key]["join"])
                    query = query.add_column(expression.label(column_key))
                elif self.column_def[column_key][
                        "join"] is not None and self.column_def[column_key][
                            "outer_join"]:
                    if not self.column_def[column_key][
                            "join"] in already_joined:
                        if self.column_def[column_key][
                                "join_expression"] is not None:
                            query = query.outerjoin(
                                self.column_def[column_key]["join"],
                                self.column_def[column_key]["join_expression"])
                        else:
                            query = query.outerjoin(
                                self.column_def[column_key]["join"])

                        already_joined.append(
                            self.column_def[column_key]["join"])
                    query = query.add_column(expression.label(column_key))

        if database_session:
            return query.with_session(database_session)
        else:
            return query