简体   繁体   中英

Django [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'MYSCHEMA.MyUnmanagedModel'

I'm running into this issue:

django.db.utils.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'MYSCHEMA.MyUnmanagedModel'.

It's an unmanaged model, so I understand that I have to create the table manually ahead of time.

Here is the model:

class MyUnmanagedModel(models.Model):
    id = models.IntegerField(db_column="ID", primary_key=True)
    end_year = models.IntegerField(db_column="endyear")
    grade = models.CharField(db_column="grade", max_length=2)
    enrollments = models.IntegerField(db_column="enrollments")

    class Meta(object):
        managed = False
        db_table = "[MYSCHEMA].[MyModelTable]"

I have ensured that my docker database contains the appropriate 'MYSCHEMA' with the table 'MyModelTable' and that it's built out appropriately with respect to the model (all the appropriate columns).

I'm using factory-boy to create the object: here is my Factory:

class MyUnmanagedModelFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = MyUnmanagedModel
        database = "secondary_database"

    id = factory.Sequence(lambda n: n)
    end_year = 2021
    grade = 1
    enrollments = 1200

I use multiple databases in my project, the 'secondary_database' is the one that holds the database with the appropriate schema.

DATABASES = {
    "default": {
        "ENGINE": "mssql",
        "NAME": os.environ.get("default_NAME"),
        "HOST": os.environ.get("default_HOST"),
        "USER": os.environ.get("default_USER"),
        "PASSWORD": os.environ.get("default_PASSWORD"),
        "PORT": os.environ.get("default_PORT"),
        "OPTIONS": {
            "host_is_server": True,
            "driver": os.environ.get("ODBC_DRIVER"),
        },
    },
    "secondary_database": {
        "ENGINE": "mssql",
        "NAME": os.environ.get("secondary_NAME"),
        "HOST": os.environ.get("secondary_HOST"),
        "USER": os.environ.get("secondary_USER"),
        "PASSWORD": os.environ.get("secondary_PASSWORD"),
        "PORT": os.environ.get("secondary_PORT"),
        "OPTIONS": {
            "host_is_server": True,
            "driver": os.environ.get("ODBC_DRIVER"),
        },
    },

Using DataGrip or Azure Data Studio, I can connect to the secondary_database, I can see the MYSCHEMA exists and I can confirm that the table 'MyModelTable' exists.

When I run MyUnmanagedModelFactory() is where I see the error:

django.db.utils.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'MYSCHEMA.MyModelTable'. (208) (SQLExecDirectW)")

Tried to set database connection extra "options" to "-c search_path=SCHEMA_NAME" with no effect.

Setting db_table = "SCHEMA_NAME].[TABLE_NAME" per model solved the problem described above. set both correct SHEMA_NAME and TABLE_NAME vars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM