我正在尝试使用 django-mssql-backend 在 Django 3.0 中创建一个数据库模型,作为 SQL Server 2019 的数据库后端。该数据库对其中包含的表使用多个架构,其中一些表是非托管的(已经存在) ,以及通过迁移从头开始创建的其他内容。为了使多个模式正常工作,我使用了在此处的另一个答案中找到的技巧,建议按如下方式格式化表名:
class MyModel(models.Model):
...
class Meta:
db_table = 'schema].[table'
Run Code Online (Sandbox Code Playgroud)
这样 SQL 就可以编译为在外部自动形成的方括号中完成架构/表定义。这样做的问题是,ForeignKey 对象使用此表名称生成其约束名称,这会导致出现无效的约束名称,从而导致在创建表完成后迁移失败,并且需要创建约束。\
它们生成如下:
[schema1].[table1_colname_id_bc165567_fk2_schema2].[table_colname]
有没有办法覆盖这种行为?如果可以通过分叉后端并添加手动编译代码来覆盖这一点,我将如何去做呢?否则,如何在使用多个模式并充分利用 Django 附带的 ORM/迁移的同时在模型中拥有外键?
我有一个 Django 应用程序,后端为 Microsoft sql 服务器。为此,我安装了“pip install django-mssql-backend”。
我扩展了用户模型并添加了一个额外的确认密码字段,我正在迁移,但我遇到了以下错误
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial...Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\core\management\__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\core\management\base.py", line 398, in execute
output = self.handle(*args, **options)
File "C:\Users\RJhaveri\Documents\Ronak\SourceCode\Development\django\retailAudit\lib\site-packages\django\core\management\base.py", line 89, in wrapped
res …Run Code Online (Sandbox Code Playgroud) 我想将我的 django 应用程序连接到 MS-SQL Server 2014 数据库。我写了这段代码来建立连接。
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST':'DESKTOP-6UNRAN0',
'PORT':'1433',
'NAME': 'MOVIE',
'COLLATION' : '',
}
}
Run Code Online (Sandbox Code Playgroud)
我已经安装了 sql_server.pyodbc
pip install django-pyodbc-azure
Run Code Online (Sandbox Code Playgroud)
如文档https://pypi.org/project/django-pyodbc-azure/ 中所述。我仍然收到错误
django.db.utils.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] 未找到数据源名称且未指定默认驱动程序 (0) (SQLDriverConnect)')
django django-pyodbc sql-server-2014 pypyodbc django-mssql-backend
当我尝试使用时model.objects.filter(mybooleanfield=True)给了我这个错误
('42000', "[42000] [Microsoft] [SQL Server Native Client 11.0] [SQL Server] A non-Boolean expression was specified in a context where a condition was expected, near 'mybooleanfield'. (4145) (SQLExecDirectW) ")
Run Code Online (Sandbox Code Playgroud)
但这只有在使用过滤器时才会发生,BooleanField如果我使用PositiveIntegerField, CharField, DateTimeField or TextField过滤器没有问题。我认为这可能适用于我使用的版本
Python 3.9.0
Django 3.1.3
Pyodbc 4.0.30
Django-mssql-backend 2.8.1
Run Code Online (Sandbox Code Playgroud)
我需要使用作为条件我的BooleanField. 有人知道为什么会这样吗?