title: Generate GUID when creating model object using SQLAlchemy (Python)
body: <p>I'm using postgres with SQLAlchemy. I want to create Profile objects and have them autogenerate a GUID. However currently my profile IDs don't store any values, e.g:</p>
  
  <pre><code>profile = Profile(name='some_profile')
  -&gt; print(profile.name)
  some_profile
  -&gt; print(profile.id)
  None
  </code></pre>
  
  <p>I've looked into how others are implementing GUIDs into their models (<a href="https://stackoverflow.com/questions/183042/how-can-i-use-uuids-in-sqlalchemy">How can I use UUIDs in SQLAlchemy?</a>)
  I understand that many people don't recommend using GUIDs as IDs, but I would like to know where I'm going wrong despite this.</p>
  
  <p>Here's my current implementation:</p>
  
  <pre><code>from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy import Column, String
  from sqlalchemy.types import TypeDecorator, CHAR
  import uuid
  
  Base = declarative_base()
  
  class GUID(TypeDecorator):
      """Platform-independent GUID type.
  
      Uses Postgresql's UUID type, otherwise uses
      CHAR(32), storing as stringified hex values.
  
      """
      impl = CHAR
  
      def process_bind_param(self, value, dialect):
          if value is None:
              return value
          elif dialect.name == 'postgresql':
              return str(value)
          else:
              if not isinstance(value, uuid.UUID):
                  return "%.32x" % uuid.UUID(value).int
              else:
                  # hexstring
                  return "%.32x" % value.int
  
      def process_result_value(self, value, dialect):
          if value is None:
              return value
          else:
              if not isinstance(value, uuid.UUID):
                  value = uuid.UUID(value)
              return value
  
  
  
  class Profile(Base):
      __tablename__ = 'profile'
  
      id = Column(GUID(), primary_key=True, default=uuid.uuid4)
      name = Column(String)
  </code></pre>
  
  <p>I'm still a beginner to python, but as far as I understand I'm declaring the type of my Profile id column as a GUID (setup by the GUID class). A default GUID value should therefore be successfully stored when generated in that column through uuid.uuid4().</p>
  
  <p>My guess is that there isn't anything wrong with the GUID class, but instead in how I'm trying to generate the default value within the id column.</p>
  
  <p>Any help would be appreciated!</p>