0

I have a Flask application which uses SQLAlchemy to store its data

I have a model AdminModel as shown below:

admin.py

class AdminModel(database.db.Model):
__tablename__ = "admins"

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

init.py:

def create_default_admin():
    keys = settings.TwitterSettings.get_instance().super_admin # returns ["adminemail", "adminusername", "adminpassword"]
    admin = admin_mod.AdminModel(keys[0], keys[1], keys[2])
    admin.insert()


def create_app():
    # Create a Flask Application
    app = Flask(
        __name__,
        template_folder="templates",
        static_folder="static",
        instance_relative_config=True,
    )

    # Register blueprints
    app.register_blueprint(admin.admin, url_prefix="/admin")
    app.register_blueprint(live.live, url_prefix="/web")
    app.register_blueprint(api.api_bp, url_prefix="/api")

    # Flask configurations
    # app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///databases/fortweets.db"
    app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
        "DATABASE_URL", f"sqlite:///databases/fortweets.db"
    )
    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
    app.config["PROPAGATE_EXCEPTIONS"] = 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="*")

# Creates default admins and insert in db
create_default_admin()

...


# Start the app
if __name__ == "__main__":
    socketio.run(app, host="0.0.0.0")

When i run this app locally on my laptop, it works fine.

When i run it on Heroku (With postgres database add-on in Heroku) i get the following error in logs:

2020-12-04T11:04:57.005331+00:00 app[web.1]: sqlalchemy.exc.IntegrityError: 

(psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint "pg_type_typname_nsp_index"
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]: ]

Can someone tell me what I'm doing wrong please

Why is it working locally with sqlite3 but not Heroku Postgresql?

2 Answers 2

1

I also faced the same issue. Well, the simple solution to this was to restart the Heroku dynos via this command:

heroku restart

This restarted the dynos and created the tables without any error.

Sign up to request clarification or add additional context in comments.

Comments

0

After searching for hours, i switched to UWSGI instead of gunicorn and it worked.

If anyone knows why gunicorn shouldn't work for this, pelase let me know.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.