Python 无法从Heroku上的Django连接到SQL Server

Python 无法从Heroku上的Django连接到SQL Server,python,sql-server,django,heroku,django-pyodbc-azure,Python,Sql Server,Django,Heroku,Django Pyodbc Azure,我的网站将从SQLServer加载一个现有的表(品牌),并显示到索引页面。我可以在我的机器上运行这个网站,没有任何问题,但是当我将项目部署到Heroku时,这个表似乎不存在 (如果网站未连接到SQL Server,部署将正常工作。) 以下是我的设置: runtime.txt python-3.7.0 django pyodbc gunicorn django-heroku django-pyodbc-azure Procfile web: gunicorn project_heroku.ws

我的网站将从SQLServer加载一个现有的表(品牌),并显示到索引页面。我可以在我的机器上运行这个网站,没有任何问题,但是当我将项目部署到Heroku时,这个表似乎不存在

(如果网站未连接到SQL Server,部署将正常工作。)

以下是我的设置: runtime.txt

python-3.7.0
django
pyodbc
gunicorn
django-heroku
django-pyodbc-azure
Procfile

web: gunicorn project_heroku.wsgi --log-file -
unixodbc
unixodbc-dev
python-pyodbc
libsqliteodbc
设置.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,    
        }
    }
}
class Brand(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=60)

    class Meta:
        managed = False
        db_table = 'brand'
from .models import Brand

def index(request):
    brand = list(Brand.objects.all().values())
    context = {
        'brand': brand
    }
    return render(request, 'index.html', context)
型号.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,    
        }
    }
}
class Brand(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=60)

    class Meta:
        managed = False
        db_table = 'brand'
from .models import Brand

def index(request):
    brand = list(Brand.objects.all().values())
    context = {
        'brand': brand
    }
    return render(request, 'index.html', context)
视图.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,    
        }
    }
}
class Brand(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=60)

    class Meta:
        managed = False
        db_table = 'brand'
from .models import Brand

def index(request):
    brand = list(Brand.objects.all().values())
    context = {
        'brand': brand
    }
    return render(request, 'index.html', context)
我在Heroku上添加了2个构建包

  • heroku/python
  • 在我的Aptfile中

    web: gunicorn project_heroku.wsgi --log-file -
    
    unixodbc
    unixodbc-dev
    python-pyodbc
    libsqliteodbc
    
    在my requirements.txt中

    python-3.7.0
    
    django
    pyodbc
    gunicorn
    django-heroku
    django-pyodbc-azure
    
    软件包已成功构建,但网站给出了此错误

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

    请帮忙!谢谢

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

    更新:
    我发现Heroku会自动创建并附加我不想要的postgresql。因此,我删除了postgresql,错误更改为无法打开库“SQL Server的ODBC驱动程序17”:未找到文件:(我想我必须对数据库_urlconfig做些什么。

    你做了
    heroku运行manage.py migrate
    ?是的(migrate和makemigrations)。在日志中,它说所有用户登录表都已创建(我没有使用它们,它们已经存在)。问题仍然是,您是否在生产中使用差异数据库?您确定数据库中存在表吗?是的,我在我的机器上运行完全相同的代码,并且工作正常。好的,我发现Heroku自动创建并附加了我不想要的postgresql。因此,我删除了postgresql,错误更改为“无法打开库”SQL Server的ODBC驱动程序17”:未找到文件。:(我想我必须对数据库进行URL配置。您是否执行了
    heroku run manage.py migrate
    ?是的(包括migrate和makemigrations)。在日志中,它说所有用户登录表都已创建(我没有使用它们,它们已经存在)。问题仍然是,您是否在生产中使用差异数据库?您确定数据库中存在表吗?是的,我在我的机器上运行完全相同的代码,并且工作正常。好的,我发现Heroku自动创建并附加了我不想要的postgresql。因此,我删除了postgresql,错误更改为“无法打开库”SQL Server的ODBC驱动程序17':未找到文件。:(我想我必须对数据库\u URL配置进行一些操作。