title: flask sqlalchemy postgres model issue:How to avoid entering "id" manually
body: <p>I have a Model, a Blueprint, and some Issues I need to solve about this code.</p>
  <h3>MODEL</h3>
  <pre class="lang-py prettyprint-override"><code>from datetime import datetime
  from app import db
  
  
  class FeedItem(db.Model):
      __tablename__ = &quot;feed_item&quot;
  
      id = db.Column(db.Integer, primary_key=True)
      caption = db.Column(db.String(255), index=True, unique=True, primary_key=True)
      url = db.Column(db.String(255), index=True, unique=True, primary_key=True)
      createdAt = db.Column(db.DateTime, index=True, default=datetime.utcnow)
      updatedAt = db.Column(db.DateTime, index=True, default=datetime.utcnow)
  
      
      def from_dict(self, data):
          for field in ['caption', 'url']:
              if field in data:
                  setattr(self, field, data[field])
  
      def addNewFeed(feed):
          &quot;&quot;&quot;Add a new test (test)&quot;&quot;&quot;
          db.session.add(feed)
          db.session.commit()
  
      @classmethod
      def createFeed(cls, caption, url):
          &quot;&quot;&quot;Create a new feed (caption, url)&quot;&quot;&quot;
          newFeed = cls(
              caption=caption,
              url=url
          )
          db.session.add(newFeed)
          db.session.commit()
          return newFeed
  </code></pre>
  <h3>BLUEPRINT</h3>
  <pre class="lang-py prettyprint-override"><code>@v1.route(&quot;/feed&quot;, methods=[&quot;POST&quot;])
  @v0.route(&quot;/feed&quot;, methods=[&quot;POST&quot;])
  def post_feed():
      data = request.get_json() or {}
      if 'caption' not in data or 'url' not in data:
          return bad_request('must include caption and url fields')
  
      # Code that needs to be fixed
      feed = FeedItem()
      feed.caption = data['caption']
      feed.url = data['url']
      feed.id = random.randint(100, 99999)
      db.session.add(feed)
      db.session.commit()
      # End of code that needs to be fixed
  
      feed.url = create_presigned_url(feed.url)
      return jsonify({
          &quot;caption&quot;: feed.caption,
          &quot;url&quot;: feed.url
      })
  </code></pre>
  ISSUE:
  <p>Above blue prints work perfectly.
  However as you can see I'm creating the feed.id by myself and that is suboptimal</p>
  <p>Now lets say I try to substitute these line of code above :</p>
  <pre class="lang-py prettyprint-override"><code># Code that needs to be fixed
  FeedItem()
  feed.caption = data['caption']
  feed.url = data['url']
  feed.id = random.randint(100, 99999)
  db.session.add(feed)
  db.session.commit()
  # End of code that needs to be fixed
  </code></pre>
  <p>With any of these 3 options:</p>
  <p>OPTION 1</p>
  <pre class="lang-py prettyprint-override"><code>feed = FeedItem(caption=data['caption'], url=data['url'])
  db.session.add(feed)
  db.session.commit()
  </code></pre>
  <p>OPTION 2</p>
  <pre class="lang-py prettyprint-override"><code>feed = FeedItem()
  feed.from_dict(data)
  db.session.add(feed)
  db.session.commit()
  </code></pre>
  <p>OPTION 3</p>
  <pre class="lang-py prettyprint-override"><code>feed = FeedItem()
  feed.caption = data['caption']
  feed.url = data['url']
  db.session.add(feed)
  db.session.commit()
  </code></pre>
  <p>For all cases I will always get the the following error that states:</p>
  <p>&quot;..null value in column &quot;id&quot; violates not-null constraint..&quot;</p>
  <pre><code>sqlalchemy.exc.IntegrityError: (psycopg2.errors.NotNullViolation) null value in column &quot;id&quot; violates not-null constraint
  DETAIL:  Failing row contains (null, DogPicture34, my_small_dog34jpg, 2020-06-30 18:51:00.679511, 2020-06-30 18:51:00.679518).
  
  [SQL: INSERT INTO feed_item (caption, url, &quot;createdAt&quot;, &quot;updatedAt&quot;) VALUES (%(caption)s, %(url)s, %(createdAt)s, %(updatedAt)s)]
  [parameters: {'caption': 'DogPicture34', 'url': 'my_small_dog34jpg', 'createdAt': datetime.datetime(2020, 6, 30, 18, 51, 0, 679511), 'updatedAt': datetime.datetime(2020, 6, 30, 18, 51, 0, 679518)}]
  (Background on this error at: http://sqlalche.me/e/13/gkpj)
  </code></pre>
  <h3>QUESTIONS</h3>
  <p>1 -What is the best approach to solve this issue ( What should I do for not having to enter the id manually?)
  What is the best pattern to follow ?</p>