Paul Miranda
Paul Miranda

Reputation: 748

Cannot change column 'id': used in a foreign key constraint of table error on Django

I'm trying to create models taking a MySQL database as reference using Django, but when I try to migrate, it throws me the following error:

Cannot change column 'id': used in a foreign key constraint 'fk_post' of table 'demoniosmv.images'"

My models look like this:

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone

class Images(models.Model):
    #id = models.IntegerField(primary_key=True)
    fk_post = models.ForeignKey('Post', related_name="images", blank=True, null=True, on_delete = models.CASCADE)
    img_url = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = True


class Post(models.Model):
    #id = models.IntegerField(primary_key=True)
    author = models.ForeignKey(User, related_name="posts", on_delete = models.CASCADE)
    title = models.CharField(max_length=200, blank=True, null=True)
    text = models.TextField(blank=True, null=True)
    created_date = models.DateTimeField(default = timezone.now)

    class Meta:
        managed = True

And the SQL script from which I'm creating the database is this:

-- MySQL Script generated by MySQL Workbench
-- Sun Jul 21 14:14:44 2019
-- Model: New Model    Version: 1.0
-- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- -----------------------------------------------------
-- Schema demoniosmv
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema demoniosmv
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `demoniosmv` DEFAULT CHARACTER SET utf8 ;
USE `demoniosmv` ;

-- -----------------------------------------------------
-- Table `demoniosmv`.`post`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `demoniosmv`.`post` (
  `id` INT NOT NULL,
  `title` VARCHAR(200) NULL,
  `text` TEXT(1000) NULL,
  `created_date` DATETIME NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `demoniosmv`.`images`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `demoniosmv`.`images` (
  `id` INT NOT NULL,
  `fk_post` INT NULL,
  `img_url` VARCHAR(100) NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_post_idx` (`fk_post` ASC) VISIBLE,
  CONSTRAINT `fk_post`
    FOREIGN KEY (`fk_post`)
    REFERENCES `demoniosmv`.`post` (`id`)
    ON DELETE CASCADE
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

For creating the models I used the insepctdb command, I'm using sqlclient for the MySQL connection, and I'm using Django 2.2.3 with Python 3.7.

Upvotes: 2

Views: 3141

Answers (1)

Paul Miranda
Paul Miranda

Reputation: 748

I had to delete the files inside migrations and py_cache folders to make it work. All the files except for the init.py files

Upvotes: 0

Related Questions