Problems with fields of datetime in migration legacy of the database



  • There's an old MySQL database with a PHP website. I moved it. https://docs.djangoproject.com/en/1.9/howto/legacy-databases/ documentation.

    It was beautiful:

    class Articles(models.Model):
        id_art = models.BigIntegerField(primary_key=True)
        id_section = models.BigIntegerField()
        id_t = models.BigIntegerField()
        id_ct = models.BigIntegerField()
        fesid = models.IntegerField()
        postpone_until = models.DateTimeField(blank=True, null=True)
        date_art = models.DateTimeField()
        status_art = models.CharField(max_length=3, blank=True, null=True)
        title_art = models.TextField()
        meta_art = models.TextField()
        short_art = models.TextField()
        full_art = models.TextField()
        foto_art = models.TextField(blank=True, null=True)
        foto_art_text = models.TextField(blank=True, null=True)
        sort_art = models.IntegerField()
        map_art = models.TextField(blank=True, null=True)
        coordinates_art = models.TextField()
        vk_id = models.CharField(max_length=40)
        nl2br = models.IntegerField()
        id_man = models.IntegerField()
    
    class Meta:
        managed = True
        db_table = 'articles'
    

    Then I redesigned it to a new project in several migrations. Something like that:

    class Articles(models.Model):
    id_art = models.AutoField(primary_key=True)
    title_art = models.CharField(verbose_name='Заголовок', max_length=250)
    id_section = TreeForeignKey(SectionArt, db_column='id_section', verbose_name='Категория')
    fesid = models.ForeignKey(Fedsubj, db_column='fesid', verbose_name='Регион')
    id_ct = ChainedForeignKey(
    Cities,
    chained_field='fesid',
    chained_model_field='fesid',
    show_all=False,
    auto_choose=True,
    db_column='id_ct',
    verbose_name='Город'
    )
    created_at = models.DateTimeField(
    verbose_name='Дата создания',
    blank=True,
    null=True,
    auto_now_add=True,
    db_column='date_art',
    )
    meta_art = models.TextField(null=True, blank=True, verbose_name='Мета')
    short_art = models.TextField(verbose_name='Короткое описание', max_length=300)
    full_art = RichTextUploadingField(verbose_name='Содержание')
    foto_art = models.ImageField(blank=True, null=True, verbose_name='Изображение')
    foto_art_text = models.TextField(blank=True, null=True, verbose_name='Описание изображения')
    coordinates_art = models.TextField(blank=True, null=True, verbose_name='Координаты')

    class Meta:
        managed = True
        db_table = 'articles'
        verbose_name = u'Новость'
        verbose_name_plural = u'Новости'
    
    def __unicode__(self):
        return self.title_art
    
    def category_name(self):
        return self.id_section.get_root().title_sec
    
    category_name.short_description = u'Раздел'
    

    And now there's something very strange about migration:

    django.db.utils.OperationalError: (1292, "Incorrect datetime value: '0000-00-00 00:00:00' for column 'postpone_until' at row 3595")

    Even though I've already removed postpone_until' in my migration. By the way, here she is:</p><pre><code>operations = [ migrations.RemoveField( model_name='articles', name='date_art', ), migrations.RemoveField( model_name='articles', name='id_man', ), migrations.RemoveField( model_name='articles', name='id_t', ), migrations.RemoveField( model_name='articles', name='map_art', ), migrations.RemoveField( model_name='articles', name='nl2br', ), migrations.RemoveField( model_name='articles', name='postpone_until', ), migrations.RemoveField( model_name='articles', name='sort_art', ), migrations.RemoveField( model_name='articles', name='status_art', ), migrations.RemoveField( model_name='articles', name='vk_id', ), migrations.AddField( model_name='articles', name='created_at', field=models.DateTimeField(auto_now_add=True, db_column='date_art', null=True, verbose_name='\u0414\u0430\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f'), ), migrations.AlterField( model_name='articles', name='coordinates_art', field=models.TextField(blank=True, null=True, verbose_name='\u041a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b'), ), migrations.AlterField( model_name='articles', name='fesid', field=models.ForeignKey(db_column='fesid', on_delete=django.db.models.deletion.CASCADE, to='articles.Fedsubj', verbose_name='\u0420\u0435\u0433\u0438\u043e\u043d'), ), migrations.AlterField( model_name='articles', name='foto_art', field=models.ImageField(blank=True, null=True, upload_to=b'', verbose_name='\u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435'), ), migrations.AlterField( model_name='articles', name='foto_art_text', field=models.TextField(blank=True, null=True, verbose_name='\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f'), ), migrations.AlterField( model_name='articles', name='full_art', field=ckeditor_uploader.fields.RichTextUploadingField(verbose_name='\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435'), ), migrations.AlterField( model_name='articles', name='id_art', field=models.AutoField(primary_key=True, serialize=False), ), migrations.AlterField( model_name='articles', name='id_ct', field=smart_selects.db_fields.ChainedForeignKey(auto_choose=True, chained_field='fesid', chained_model_field='fesid', db_column='id_ct', on_delete=django.db.models.deletion.CASCADE, to='articles.Cities', verbose_name='\u0413\u043e\u0440\u043e\u0434'), ), migrations.AlterField( model_name='articles', name='id_section', field=mptt.fields.TreeForeignKey(db_column='id_section', on_delete=django.db.models.deletion.CASCADE, to='articles.SectionArt', verbose_name='\u041a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u044f'), ), migrations.AlterField( model_name='articles', name='meta_art', field=models.TextField(blank=True, null=True, verbose_name='\u041c\u0435\u0442\u0430'), ), migrations.AlterField( model_name='articles', name='short_art', field=models.TextField(max_length=300, verbose_name='\u041a\u043e\u0440\u043e\u0442\u043a\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435'), ), migrations.AlterField( model_name='articles', name='title_art', field=models.CharField(max_length=250, verbose_name='\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a'), ), ] </code></pre><p>How do you fight it?</p><hr/><h2>UPD</h2><p><code>sqldiff</code> I don't know.</p><pre><code> ALTER TABLE articles DROP COLUMNid_t; ALTER TABLE articles DROP COLUMNpostpone_until; ALTER TABLE articles DROP COLUMNstatus_art; ALTER TABLE articles DROP COLUMNsort_art; ALTER TABLE articles DROP COLUMNmap_art; ALTER TABLE articles DROP COLUMNvk_id; ALTER TABLE articles DROP COLUMNnl2br; ALTER TABLE articles DROP COLUMNid_man; CREATE INDEX articles_id_section ONarticles (id_section); CREATE INDEX articles_fesid ONarticles (fesid); CREATE INDEX articles_id_ct ONarticles (id_ct); ALTER TABLE articles MODIFYid_artinteger AUTO_INCREMENT; ALTER TABLEarticles MODIFYtitle_artvarchar(250); ALTER TABLEarticles MODIFYid_sectioninteger; ALTER TABLEarticles MODIFYfoto_artvarchar(100); ALTER TABLEarticles MODIFYdate_artDROP NOT NULL; ALTER TABLEarticles MODIFYmeta_artDROP NOT NULL; ALTER TABLEarticles MODIFYcoordinates_art` DROP NOT NULL;



  • I found the reason for the error: postpone_until one of the records was kept 00.00.0000 00:00:00♪ After manual removal, the migration started. But that's all the dates. date_art We're out.


    ALTER TABLE `articles`
            DROP COLUMN `id_t`;
    ALTER TABLE `articles`
            DROP COLUMN `postpone_until`;
    ALTER TABLE `articles`
            DROP COLUMN `status_art`;
    ALTER TABLE `articles`
            DROP COLUMN `sort_art`;
    ALTER TABLE `articles`
            DROP COLUMN `map_art`;
    ALTER TABLE `articles`
            DROP COLUMN `vk_id`;
    ALTER TABLE `articles`
            DROP COLUMN `nl2br`;
    ALTER TABLE `articles`
            DROP COLUMN `id_man`;
    CREATE INDEX `articles_id_section`
            ON `articles` (`id_section`);
    CREATE INDEX `articles_fesid`
            ON `articles` (`fesid`);
    CREATE INDEX `articles_id_ct`
            ON `articles` (`id_ct`);
    ALTER TABLE `articles`
            MODIFY `id_art` integer AUTO_INCREMENT;
    ALTER TABLE `articles`
            MODIFY `title_art` varchar(250);
    ALTER TABLE `articles`
            MODIFY `id_section` integer;
    ALTER TABLE `articles`
            MODIFY `foto_art` varchar(100);
    ALTER TABLE `articles`
            MODIFY `date_art` DROP NOT NULL;
    ALTER TABLE `articles`
            MODIFY `meta_art` DROP NOT NULL;
    ALTER TABLE `articles`
            MODIFY `coordinates_art` DROP NOT NULL;
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2