1

My website will just load an existing table (brand) from SQL server and display to index page. I'm able to run the website on my machine without any problem, but it seems like the table doesn't exist when I deploy the project to Heroku.

(The deploy is working normally if the website doesn't connect to the SQL Server.)

These are my settings:

runtime.txt

python-3.7.0

Procfile

web: gunicorn project_heroku.wsgi --log-file -

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': DB_DEFAULT_NAME,
        'USER': DB_USER,
        'PASSWORD': DB_PASSWORD,
        'HOST': DB_HOST,
        'PORT': '',
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
            'unicode_results': True,    
        }
    }
}

models.py

class Brand(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=60)

    class Meta:
        managed = False
        db_table = 'brand'

views.py

from .models import Brand

def index(request):
    brand = list(Brand.objects.all().values())
    context = {
        'brand': brand
    }
    return render(request, 'index.html', context)

I added 2 build packs on Heroku

  1. https://github.com/heroku/heroku-buildpack-apt
  2. heroku/python

In my Aptfile

unixodbc
unixodbc-dev
python-pyodbc
libsqliteodbc

In my requirements.txt

django
pyodbc
gunicorn
django-heroku
django-pyodbc-azure

The package has been built successfully, but the website give this error

ProgrammingError at /
relation "brand" does not exist
LINE 1: SELECT "brand"."id", "brand"."name" FROM "brand"

ERROR SCREENSHOT

Please help! Thanks!!

============================

Update:

I found that Heroku automatically create and attach postgresql which I didn't want. Therefore, I deleted the postgresql and the error is changed to Can't open lib 'ODBC Driver 17 for SQL Server' : file not found. :( I guess that I have to do something with DATABASE_URL config.

Lazywii
  • 133
  • 1
  • 7
  • Did you do `heroku run manage.py migrate`? – Daniel Roseman Feb 11 '19 at 08:40
  • Yes I did (both migrate and makemigrations). In the log, it said that all user login tables have been created (I didn't use them, and they were already exist). The problem is still there – Lazywii Feb 11 '19 at 09:21
  • Are you using difference database on production? Are you sure table exists in database? – Jrog Feb 11 '19 at 09:58
  • Yes, I run exactly the same code on my machine, and it works fine. – Lazywii Feb 11 '19 at 13:14
  • OK, I found that Heroku automatically create and attach postgresql which I didn't want. Therefore, I deleted the postgresql and the error is changed to Can't open lib 'ODBC Driver 17 for SQL Server' : file not found. :( I guess that I have to do something with DATABASE_URL config. – Lazywii Feb 12 '19 at 09:02

0 Answers0