1

Application works fine locally, but I am having issues deploying my application on Heroku, getting an Internal Server Error. When I run heroku logs --tail I get this:

2020-04-24T22:39:36.957365+00:00 app[web.1]: sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "farm" does not exist
    2020-04-24T22:39:36.957366+00:00 app[web.1]: LINE 2: FROM farm ORDER BY farm.name
    2020-04-24T22:39:36.957366+00:00 app[web.1]: ^
    2020-04-24T22:39:36.957366+00:00 app[web.1]:
    2020-04-24T22:39:36.957367+00:00 app[web.1]: [SQL: SELECT farm.id AS farm_id, farm.name AS farm_name, farm.address AS farm_address, farm.city AS farm_city
    2020-04-24T22:39:36.957367+00:00 app[web.1]: FROM farm ORDER BY farm.name]
    2020-04-24T22:39:36.957367+00:00 app[web.1]: (Background on this error at: http://sqlalche.me/e/f405)
    2020-04-24T22:39:36.957668+00:00 app[web.1]: 10.95.177.213 - - [24/Apr/2020:22:39:36 +0000] "GET /farms HTTP/1.1" 500 0 "-" "
-"

Also, I am getting this status, not sure if it helps.

$ heroku pg:info
=== DATABASE_URL
Plan:                  Hobby-dev
Status:                Available
Connections:           2/20
PG Version:            12.2
Created:               2020-04-24 22:23 UTC
Data Size:             7.9 MB
Tables:                0
Rows:                  0/10000 (In compliance)
Fork/Follow:           Unsupported
Rollback:              Unsupported
Continuous Protection: Off
Add-on:                postgresql-encircled-56224

And doing a heroku diagnose:

$ heroku pg:diagnose
Report 7834cb14-1b08-4532-b611-4b457e00410e for fsdn-farm-shop::DATABASE_URL
available for one month after creation on 2020-04-25T10:06:21.528717+00:00

RED: Idle in Transaction
Pid    Duration         Query
─────  ───────────────  ────────────────────────────────────────────────────────────────────────────────────────────────────────
18615  00:20:55.869121  SELECT farm.id AS farm_id, farm.name AS farm_name, farm.address AS farm_address, farm.city AS farm_city
                        FROM farm ORDER BY farm.name
GREEN: Connection Count
GREEN: Long Queries
GREEN: Long Transactions
GREEN: Indexes
GREEN: Bloat
GREEN: Hit Rate
GREEN: Blocking Queries
GREEN: Table Transaction ID Wraparound
GREEN: Schema Count
SKIPPED: Sequences
Error could not do check
SKIPPED: Database Transaction ID Wraparound
Error Database wraparound check not supported on this plan
SKIPPED: Load
Error Load check not supported on this plan

Models are set up this way:

class Farm(db.Model):
    __tablename__ = 'farm'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(120))
    address = db.Column(db.String(120))
    city = db.Column(db.String(32))

    product = db.relationship('Product', backref='farm', lazy=True)


class Product(db.Model):
    __tablename__ = 'product'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(120))
    quantity = db.Column(db.Integer)

    farm_id = db.Column(db.Integer, db.ForeignKey('farm.id'))

It looks like the migration ran, but I am thinking maybe there is something wrong with the database url or maybe there is a circular import. I'm new to this, can't seem to figure this out.

Also including my github: https://github.com/victorcocuz/fsdn-farm-shop

Victor Cocuz
  • 96
  • 1
  • 10

1 Answers1

0

The error message relation "farm" does not exist is being raised by postgresql, not sqlalchemy or psycopg2 (the database driver sqlalchemy uses). The error is telling us that the query the database is getting is:

SELECT 
   farm.id AS farm_id,
   farm.name AS farm_name, 
   farm.address AS farm_address, 
   farm.city AS farm_city
FROM 
   farm 
ORDER BY 
   farm.name;

but it doesn't recognize the table name farm in the schema its searching.

Note the same error in this question: Postgresql tables exists, but getting "relation does not exist" when querying

If your schema is not the public schema, I would try specifying the schema in your ORM by adding this:__table_args__ = {"schema":"schema_name"} under __tablename__ = 'farm'.

Or you could try __tablename__ = '{schema_name}.farm'.

Jordan
  • 868
  • 1
  • 8
  • 25