title: SqlAlchemy query result outputting
body: <p>I am trying to query one of my tables in my Postgres database using SqlAlchemy in Python 3. It runs the query fine but as I go through each row in the result that SqlAlchemy returns, I try to use the attribute 'text' (one of my column names). I receive this error:</p>
  
  <pre><code>'str' object has no attribute 'text'
  </code></pre>
  
  <p>I have printed the attribute like so:</p>
  
  <pre><code>for row in result:
     print(row.text)
  </code></pre>
  
  <p>This does not give the error. The code that produces the error is below. However, to give my environment:</p>
  
  <p>I have two servers running. One is for my database the other is for my python server.</p>
  
  <p><strong>Database Server:</strong></p>
  
  <ul>
  <li><code>Postgres v9.6</code> - On Amazon's RDS</li>
  </ul>
  
  <p><strong>Server with Python</strong></p>
  
  <ul>
  <li><code>Linux 3.13.0-65-generic x86_64</code> - On an Amazon EC2 Instance</li>
  <li><code>SqlAlchemy v1.1.5</code></li>
  <li><code>Python v3.4.3</code></li>
  <li><code>Flask 0.11.1</code></li>
  </ul>
  
  <p>Files related:</p>
  
  <pre><code>import sqlalchemy as sa
  from sqlalchemy.ext.automap import automap_base
  from sqlalchemy.orm import Session
  import re
  from nltk import sent_tokenize
  
  class DocumentProcess:
    def __init__(self):
      ...
      Engine = sa.create_engine(
          CONFIG.POSTGRES_URL,
          client_encoding='utf8',
          pool_size=20,
          max_overflow=0
      )
  
      # initialize SQLAlchemy
      Base = automap_base()
  
      # reflect the tables
      Base.prepare(Engine, reflect=True)
  
      # Define all needed tables
      self.Document = Base.classes.documents
  
      self.session = Session(Engine)
      ...
  
    def process_documents(self):
      try:
          offset = 5
          limit = 50
          ###### This is the query in question ##########
          result = self.session.query(self.Document) \
              .order_by(self.Document.id) \
              .offset(offset) \
              .limit(limit)
          for row in result:
            # The print statement below does print out the text
            print(row.text)
            # when passing document.text to sent_tokenize, it
            # gives the following error:
            # 'str' object has no attribute 'text'
            snippets = sent_tokenize(row.text.strip('\n')) # I have removed strip, but the same problem
      except Exception as e:
          logging.info(format(e))
          raise e
  </code></pre>
  
  <p>This is my model for Document, in my PostgreSQL database:</p>
  
  <pre><code>class Document(db.Model):
      __tablename__ = "documents"
  
      id = db.Column(db.Integer, primary_key=True)
      text = db.Column(db.Text)
      tweet = db.Column(db.JSON)
      keywords = db.Column(db.ARRAY(db.String), nullable=True)
  
      def to_dict(self):
          return dict(
              id=self.id,
              text=self.text,
              tweet=self.tweet,
              keywords=self.keywords
          )
  
      def json(self):
          return jsonify(self.to_dict())
  
      def __repr__(self):
          return "&lt;%s %r&gt;" % (self.__class__, self.to_dict())
  </code></pre>
  
  <p><strong>Things I have tried</strong></p>
  
  <ol>
  <li><p>Before, I did not have <code>order_by</code> in the Document query. This was working before. However, even removing <code>order_by</code> does not fix it anymore.</p></li>
  <li><p>Used a SELECT statement and went through the result manually, but still the same result</p></li>
  </ol>
  
  <p><strong>What I haven't tried</strong></p>
  
  <ol>
  <li>I am wondering if its because I named the column 'text'. I noticed that when I write this query out in postgres, it highlights it as a reserved word. I'm confused why my query worked before, but now it doesn't work. Could this be the issue?</li>
  </ol>
  
  <p>Any thoughts on this issue would be much appreciated.</p>