title: Flask + sqlalchemy - NameError: global name 'string is not defined
body: <p>Cannot figure out why this exception is being thrown only sometimes - maybe one in five attempts. </p>
  
  <p>I have a simple API that accepts a file, stores it, and logs some metadata in a postgres database.  </p>
  
  <p>Here's the log:</p>
  
  <pre><code>==&gt; /var/log/apache2/error.log &lt;==
  [Fri Oct 03 20:32:02.016347 2014] [:error] [pid 28259] ERROR:Exception on /api/v1.0/upload [POST]
  [Fri Oct 03 20:32:02.016454 2014] [:error] [pid 28259] Traceback (most recent call last):
  [Fri Oct 03 20:32:02.016496 2014] [:error] [pid 28259]   File "/usr/local/lib/python2.7/dist-    packages/flask/app.py", line 1817, in wsgi_app
  [Fri Oct 03 20:32:02.016534 2014] [:error] [pid 28259]     response = self.full_dispatch_request()
  [Fri Oct 03 20:32:02.016576 2014] [:error] [pid 28259]   File "/usr/local/lib/python2.7/dist- packages/flask/app.py", line 1477, in full_dispatch_request
  [Fri Oct 03 20:32:02.016615 2014] [:error] [pid 28259]     rv = self.handle_user_exception(e)
  [Fri Oct 03 20:32:02.016652 2014] [:error] [pid 28259]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
  [Fri Oct 03 20:32:02.016689 2014] [:error] [pid 28259]     reraise(exc_type, exc_value, tb)
  [Fri Oct 03 20:32:02.016725 2014] [:error] [pid 28259]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
  [Fri Oct 03 20:32:02.016762 2014] [:error] [pid 28259]     rv = self.dispatch_request()
  [Fri Oct 03 20:32:02.016798 2014] [:error] [pid 28259]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
  [Fri Oct 03 20:32:02.016831 2014] [:error] [pid 28259]     return self.view_functions[rule.endpoint](**req.view_args)
  [Fri Oct 03 20:32:02.016863 2014] [:error] [pid 28259]   File "__init__.py", line 95, in upload
  [Fri Oct 03 20:32:02.016895 2014] [:error] [pid 28259]     width=_file_data['Image width'],
  [Fri Oct 03 20:32:02.016939 2014] [:error] [pid 28259] NameError: global name 'string' is not defined
  </code></pre>
  
  <p>In my view:</p>
  
  <pre><code>from database import session
  from models import SomeInfo
  
  db_entry = SomeInfo(name=f.filename,
                      width=_file_data['Image width'],
                      height=_file_data['Image height'],
                      mime_type=_file_data['MIME type'],
                      duration=_file_data['Duration'],
                      owner=user.username,
                      location=os.path.join(config._upload_directory, f.filename))
  </code></pre>
  
  <p>My model:</p>
  
  <pre><code>from sqlalchemy import Column, Integer, String, Sequence, Text, DateTime, ForeignKey
  from database import Base, engine
  
  import datetime
  
  
  class SomeInfo(Base):
      __tablename__ = 'some_info'
  
      id = Column(Integer, primary_key=True)
      name = Column(String(2048))
      width = Column(String(2048))
      height = Column(String(2048))
      upload_date = Column(DateTime, default=datetime.datetime.today())
      last_modification = Column(DateTime, default=datetime.datetime.today())
      SomeMoreStuff
  
  Base.metadata.create_all(engine)
  </code></pre>
  
  <p>Sqlalchemy is using postgres as a backend.  </p>
  
  <p>Like I said, this only sporadically happens, every fifth post or so to the api.  </p>
  
  <p>Any insight would be greatly appreciated!</p>
  
  <p>Lines 85-95 as requested:</p>
  
  <pre><code>text = metadata.exportPlaintext()
  _file_data = {}
  for item in text[1:]:
      _file_data[item.split(':')[0].strip(' -')] = item.split(':')[1]
  
  _file_data['name'] = f.filename
  </code></pre>