You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django应用数据库表无法在SQL Server Management Studio中创建的问题

Troubleshooting Django Migrations Not Creating Tables in SQL Server

Let's break down the possible issues and actionable fixes based on your scenario—since you've already covered the basic reset steps, let's dive into the less obvious culprits:

1. You're not targeting the correct database in your migrate command

Since you’re using database routers, Django’s default migrate command only applies migrations to the default database alias unless you explicitly specify the target alias. If your app’s models are routed to a non-default database (e.g., sqlserver), you need to run:

python manage.py migrate <appname> --database=sqlserver -v 3

The -v 3 flag gives verbose output to confirm migrations are sent to the right database. Also double-check your router’s allow_migrate() method—even a tiny logic error here could block migrations from running against your SQL Server instance.

2. SQL Server user lacks DDL permissions

Even if the user can connect and read/write data, they might not have permission to create tables (DDL operations). To verify:

  • Open SQL Server Management Studio and log in with the same username/password from your Django settings.
  • Run a test query: CREATE TABLE test_table (id INT PRIMARY KEY);
    If this fails, grant the user CREATE TABLE permissions on the target database or assign them the db_ddladmin role.

3. Migration file has SQL Server-incompatible syntax

Check your 0001_initial.py migration for model fields that might clash with SQL Server’s constraints:

  • Avoid database-specific fields from other backends (e.g., older MySQL-only field types).
  • Ensure field lengths stay within limits: CharField(max_length=4000) works, but max_length=10000 will fail (SQL Server’s VARCHAR max is 8000 unless using max).
  • Confirm TextField is mapped to NVARCHAR(MAX) (modern drivers handle this, but older versions may not).

4. Outdated or misconfigured SQL Server driver

Stick to the officially supported mssql-django driver instead of legacy pyodbc setups. If you’re using pyodbc, verify your settings include the correct driver version:

DATABASES = {
    'sqlserver': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'your_db',
        'USER': 'your_user',
        'PASSWORD': 'your_pass',
        'HOST': 'your_host',
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
            'MARS_Connection': 'True',
        },
    }
}

Outdated drivers (like ODBC Driver 11) often cause silent migration failures—upgrade to Driver 17 or 18 if needed.

5. Migration tracking table is corrupted

Even though your django_migrations table is empty, its schema might be out of sync. Try these steps:

  1. Delete the django_migrations table entirely in SSMS.
  2. Recreate it with Django’s expected schema:
CREATE TABLE django_migrations (
    id INT IDENTITY(1,1) PRIMARY KEY,
    app NVARCHAR(255) NOT NULL,
    name NVARCHAR(255) NOT NULL,
    applied DATETIME2 NOT NULL
);
  1. Re-run your migrate command with the correct database alias—this forces Django to re-register and apply the migration.

内容的提问来源于stack exchange,提问作者Neil Faulkner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 16:17:32