django-mssql-backend - Invalid connection string attribute (0) - sql-server

OK, I'm slowly going crazy. I want to connect Django 3 to MSSQLv15 Server. Connection works if I use Trustedconnection=yes (using win credentials), it also works on Ubuntu if I'm using FreeTDS v7.4 but it won't work in Windows if I manually insert service account or personal credentials and use sql login. Is it some kind of dependency issue? I have tried various library version combos, but to no avail.
Error message is:
conn = Database.connect(connstr,
django.db.utils.InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'DOMAIN\\user'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0)")
Requiremetns.txt
asgiref 3.3.4
Django 3.2
django-mssql-backend 2.8.1
djangorestframework 3.12.4
pip 20.2.3
pyodbc 4.0.30
pytz 2021.1
setuptools 49.2.1
sqlparse 0.4.1
Not working (Win)
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'db_name',
'HOST': 'hostname',
'USER': 'DOMAIN\\user',
'PASSWORD': 'pass',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
}
}
}
Working (Win)
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'db_name',
'HOST': 'hostname',
'USER': '',
'PASSWORD': '',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
'Trusted_Connection': 'yes',
}
}
}
Working (Ubuntu)
django==2.1.0
django-pyodbc-azure-2019
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'db_name',
'HOST': 'hostname',
'USER': 'DOMAIN\\user',
'PASSWORD': 'pass',
'PORT': '1433',
'OPTIONS': {
"driver": "FreeTDS",
"host_is_server": True,
"extra_params": "tds_version=7.4",
}
}
}

ODBC Driver 17 for SQL Server doesn't seem to support authenticating domain users via username and password, where FreeTDS does (see https://stackoverflow.com/a/37702329/9461432). On a non-Windows machine, Microsoft directs you to use Kerberos.
Authenticating via username & password works fine if you're connecting to a user that uses SQL Server authentication (ie. non-domain).

Related

Django Backend with two different data sources

I am working on a React web app that will enable the user to store new data rows and edit/udpate old data rows. The reference data, for the React form, is stored in ADLS Gen1 parquet format and the app will write data to a different ADLS location in parquet format.
The question is: How can I setup the Django backend with these two different data source? I want to read some reference data from ADLS Gen1 and write and edit into another ADLS in parquet format.
You can use different databases in django settings like this below :
DATABASES = {
'DB1': {
'ENGINE': 'mssql',
'NAME': 'DB_NAME',
'USER': 'sa',
'PASSWORD': 'PASSWORD',
'HOST': 'HOST',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 11 for SQL Server',
},
},
'DB2': {
'ENGINE': 'mssql',
'NAME': 'DB_NAME',
'USER': 'sa',
'PASSWORD': 'PASSWORD',
'HOST': 'HOST',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 11 for SQL Server',
},
},
}
Then on your ORM you can use tbl.objects.using('DB1').all()

Cannot connect to my SQL Server database with NestJS

After reading the documentation, I've encountered a problem, I'm getting the error unable to connect to the database. I'm trying to connect to a local SQL Server database, using NestJS. I enabled a user sa and set its password. I also enabled TCP/IP connection and confirmed the port from SQL Server. I'm wondering what am I missing or doing wrong?
#Module({
imports: [
ConfigModule.forRoot({
envFilePath:'.env',
isGlobal: true
}),
TypeOrmModule.forRoot({
type: 'mssql',
host: 'localhost',
port: 1433,
username: 'root',
password: 'root',
database: 'test',
autoLoadEntities: true,
synchronize: true,
}),
],
controllers: [AppController, EstadoProyController],
providers: [AppService],
})
export class AppModule {}
check this items:
1- sql server running
2- check your port or host

Windows Django data source name not found and no default driver specified

I'm trying to connect to SQL Server studio v17.2
This is the code that works fine in my local computer
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'sql_server.pyodbc',
'NAME': 'database',
'HOST': '10...',
'USER': 'user',
'PASSWORD': 'password',
#'DRIVER':'ODBC Driver 17 for SQL Server'
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',}
}
}
This is the error I am getting:
windows django data source name not found and no default driver specified

Can't connect to MSSQL using django-pyodbc-azure 2.1.0.0

As the title says. Im getting this error
django.db.utils.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Heres my code
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'dbname',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'HOST',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
}
}
DATABASE_CONNECTION_POOLING = False
Check first if you have that driver installed:
import pyodbc
print(pyodbc.drivers())
Download ODBC Driver for SQL Server
Installing the Microsoft ODBC Driver for SQL Server on Linux and macOS

django-pyodbc-azure. Invalid object name 'django_session'

I need help with setting up my project. I used django-pyodbc-azure and I have this in my settings.DATABASES:
DATABASES = {
'default': { 'ENGINE': 'sql_server.pyodbc','NAME': 'DBNAME','USER': 'USER',
'PASSWORD': 'PASSWORD',
'HOST': '10.xx.xx.xx,1433',
'OPTIONS': {
'driver': 'SQL Server Native Client 10.0',
},
},
}
Sorry I don't know how to format the code above.
I don' know the problem is, but it returns an error:
ProgrammingError: ('42S02', "[42S02] [Microsoft][SQL Server Native Client 10.0][
SQL Server]Invalid object name 'django_session'. (208) (SQLExecDirectW); [42000]
[Microsoft][SQL Server Native Client 10.0][SQL Server]Statement(s) could not be
prepared. (8180)")
How do I fix this?
Try this:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'mydb',
'USER': 'user#myserver',
'PASSWORD': 'password',
'HOST': 'myserver.database.windows.net',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
},
}
source: https://pypi.python.org/pypi/django-pyodbc-azure

Resources