title: Flask SQLAlchemy Heroku error: duplicate key value violates unique constraint
body: <p>I have a Flask application which uses SQLAlchemy to store its data</p>
  <p>I have a model AdminModel as shown below:</p>
  <p>admin.py</p>
  <pre><code>class AdminModel(database.db.Model):
  __tablename__ = &quot;admins&quot;
  
  id = database.db.Column(database.db.Integer, primary_key=True)
  email = database.db.Column(database.db.String(80))
  username = database.db.Column(database.db.String(80))
  password = database.db.Column(database.db.String(80))
  
  def __init__(self, email, username, password):
      self.email = email
      self.username = username
      self.password = password
  
  ...
  
  def insert(self):
      admin = self.find_by_username(self.username)
      if admin is None:
          # Hash the password
          self.password = utils.hash(self.password)
          # Add to DB
          database.db.session.add(self)
          database.db.session.commit()
          return True
  
      return False
  </code></pre>
  <p><strong>init</strong>.py:</p>
  <pre><code>def create_default_admin():
      keys = settings.TwitterSettings.get_instance().super_admin # returns [&quot;adminemail&quot;, &quot;adminusername&quot;, &quot;adminpassword&quot;]
      admin = admin_mod.AdminModel(keys[0], keys[1], keys[2])
      admin.insert()
  
  
  def create_app():
      # Create a Flask Application
      app = Flask(
          __name__,
          template_folder=&quot;templates&quot;,
          static_folder=&quot;static&quot;,
          instance_relative_config=True,
      )
  
      # Register blueprints
      app.register_blueprint(admin.admin, url_prefix=&quot;/admin&quot;)
      app.register_blueprint(live.live, url_prefix=&quot;/web&quot;)
      app.register_blueprint(api.api_bp, url_prefix=&quot;/api&quot;)
  
      # Flask configurations
      # app.config[&quot;SQLALCHEMY_DATABASE_URI&quot;] = f&quot;sqlite:///databases/fortweets.db&quot;
      app.config[&quot;SQLALCHEMY_DATABASE_URI&quot;] = os.environ.get(
          &quot;DATABASE_URL&quot;, f&quot;sqlite:///databases/fortweets.db&quot;
      )
      app.config[&quot;SQLALCHEMY_TRACK_MODIFICATIONS&quot;] = False
      app.config[&quot;PROPAGATE_EXCEPTIONS&quot;] = True
      app.secret_key = settings.TwitterSettings.get_instance().jwt_secret_key
  
      # # SQLAlchemy configurations
      # database.db.app = app
      # database.db.init_app(app)
      # database.db.create_all()
  
      database.db.app = app
      database.db.init_app(app)
      database.db.create_all()
  
      return app
  
  
  # Create the app
  app = create_app()
  
  # JWT Configurations
  jwt = JWTManager(app)
  
  # Socket IO
  socketio = SocketIO(app, cors_allowed_origins=&quot;*&quot;)
  
  # Creates default admins and insert in db
  create_default_admin()
  
  ...
  
  
  # Start the app
  if __name__ == &quot;__main__&quot;:
      socketio.run(app, host=&quot;0.0.0.0&quot;)
  </code></pre>
  <p>When i run this app locally on my laptop, it works fine.</p>
  <p>When i run it on Heroku (With postgres database add-on in Heroku) i get the following error in logs:</p>
  <pre><code>2020-12-04T11:04:57.005331+00:00 app[web.1]: sqlalchemy.exc.IntegrityError: 
  
  (psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint &quot;pg_type_typname_nsp_index&quot;
  2020-12-04T11:04:57.005331+00:00 app[web.1]: DETAIL:  Key (typname, typnamespace)=(admins_id_seq, 2200) already exists.
  2020-12-04T11:04:57.005331+00:00 app[web.1]:
  2020-12-04T11:04:57.005331+00:00 app[web.1]: [SQL:
  2020-12-04T11:04:57.005332+00:00 app[web.1]: CREATE TABLE admins (
  2020-12-04T11:04:57.005332+00:00 app[web.1]: id SERIAL NOT NULL,
  2020-12-04T11:04:57.005332+00:00 app[web.1]: email VARCHAR(80),
  2020-12-04T11:04:57.005332+00:00 app[web.1]: username VARCHAR(80),
  2020-12-04T11:04:57.005333+00:00 app[web.1]: password VARCHAR(80),
  2020-12-04T11:04:57.005333+00:00 app[web.1]: PRIMARY KEY (id)
  2020-12-04T11:04:57.005333+00:00 app[web.1]: )
  2020-12-04T11:04:57.005334+00:00 app[web.1]:
  2020-12-04T11:04:57.005334+00:00 app[web.1]: ]
  </code></pre>
  <p>Can someone tell me what I'm doing wrong please</p>
  <p>Why is it working locally with sqlite3 but not Heroku Postgresql?</p>