I am working with MSSQL database and I have some tables that were already created before Django enters the show, so with inspectdb I got the models with managed=False Meta option. Then I create others that work with Django migrations. This models are something like this:
class ModelAlreadyCreated(models.Model):
# This PK field is set as varchar(16)
id = models.CharField(primary_key=True, db_column='CVE', max_length=16)
...other fields
class Meta:
managed=False
class DjangoModel(models.Model):
model_ac_fk = models.ForeignKey(ModelAlreadyCreated)
... other fields
When I create the migrations file using makemigrations I get this:
migrations.CreateModel(
name='DjangoModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('model_ac_fk', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ModelAlreadyCreated')),
...
],
),
Until this point everything seems ok, but with the migrate command this error is shown:
django.db.utils.OperationalError: (1778, "Column 'CVE' is not the same data
type as referencing column 'model_ac_fk' in foreign key 'model_ac_fk_fk_CVE'.DB-Lib error
message 20018, severity 16:\nGeneral SQL Server error: Check messages from
the SQL Server\nDB-Lib error message 20018, severity 16:\nGeneral SQL Server
error: Check messages from the SQL Server\n")
This error comes out because Django is trying to create a nvarchar(16) foreignkey field and the actual referenced field is varchar(16)
I can get the SQL using sqlmigrate and alter the field types, then run it directly in the DB and Fake the migration, but I want to know if there is any way to make the migration using Django without touching any SQL script?