Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

380
Vistas
Peewee and postgres on heroku, not able to create table

I tried without database my app working great but with peewee and postgres, Its not able to create table at all.

my app

I tried different methods posted online but its not working, anyone if you have used peewee on heroku please help.

My model.py

if os.environ.get('DATABASE_URL'):
    DATABASE_URL = os.environ.get('DATABASE_URL')
    db = urlparse(DATABASE_URL)
    user = db.username
    password = db.password
    path = db.path[1:]
    host = db.hostname
    port = db.port
    database = PostgresqlDatabase(path, user=user, password=password, host=host, port=port)
else:
    database = PostgresqlDatabase('heroku')


class BaseModel(Model):
    class Meta:
        database = database


class User(BaseModel):
    name = CharField()
    email = CharField(null=False, unique=True)

After moving create table from if statement to welcome function table is created.

@app.route('/')
def welcome():
    call('printenv')
    a = 'Default'
    b = 'Default'
    if os.environ.get('DATABASE_URL'):
        a = os.environ.get('DATABASE_URL')
    if os.environ.get('HEROKU'):
        b = os.environ.get('HEROKU')
    create_model_tables([Users], fail_silently=True)
    Users.insert(name='John', email='Doe').execute()
    return render_template('index.html', a=a, b=b)


if __name__ == '__main__':
    app.run(port=5000, debug=True)
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

"user" is a reserved table -- try:

class User(Model):
    ...
    class Meta:
        db_table = 'users'
over 4 years ago · Santiago Trujillo Denunciar

0

from playhouse.db_url import connect
import settings

local_url = 'postgresql://{}:{}@localhost:5432/{}'.format(settings.DB_USER, settings.DB_PASS, settings.DB_NAME)

database = connect(os.environ.get('DATABASE_URL') or local_url)


class BaseModel(Model):
    class Meta:
        database = database

Use this way, its easy and there is very few chance of getting it wrong.

And you must create table before if __name__ == '__main__:, this statement, because on heroku you give settings such that, anything under that wont work.

Like mine uswgi.ini

[uwsgi]
http-socket = :$(PORT)
master = true
die-on-term = true
module = app:app
memory-report = true

module = app:app means run the app from app.py not run app.py

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda