cherrylatte cherrylatte - 24 days ago 9
Python Question

having trouble with mysql migration (can't multiply sequence by non-int of type 'tuple')

I am havnig troulble about migrating..I first tried


python manage.py migrate qablog


and didn't work. so I tried

python manage.py migrate qablog --fake-initial


and didn't work too.. so I googled again and some people were talking about changing the settings.py so I changed some things like changing USE_TZ to false ..


python manage.py makemigrations qablog


worked well though...can anyone help me with this? ..
python version: 3.4.4
mysql server version: 5.7
django version: 1.10.3

The error log is like below:

C:\inetpub\wwwroot\test>python manage.py migrate qablog --fake-initial
Operations to perform:
Apply all migrations: qablog
Running migrations:
Applying contenttypes.0001_initial... FAKED
Applying auth.0001_initial... OK
Applying qablog.0001_initial... OK
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
367, in execute_from_command_line
utility.execute()
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 294,
in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 345,
in execute
output = self.handle(*args, **options)
File "C:\Python34\lib\site-packages\django\core\management\commands\migrate.py
", line 224, in handle
self.verbosity, self.interactive, connection.alias, apps=post_migrate_apps,
plan=plan,
File "C:\Python34\lib\site-packages\django\core\management\sql.py", line 53, i
n emit_post_migrate_signal
**kwargs
File "C:\Python34\lib\site-packages\django\dispatch\dispatcher.py", line 191,
in send
response = receiver(signal=self, sender=sender, **named)
File "C:\Python34\lib\site-packages\django\contrib\auth\management\__init__.py
", line 83, in create_permissions
Permission.objects.using(using).bulk_create(perms)
File "C:\Python34\lib\site-packages\django\db\models\query.py", line 452, in b
ulk_create
ids = self._batched_insert(objs_without_pk, fields, batch_size)
File "C:\Python34\lib\site-packages\django\db\models\query.py", line 1068, in
_batched_insert
self._insert(item, fields=fields, using=self.db)
File "C:\Python34\lib\site-packages\django\db\models\query.py", line 1045, in
_insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 10
53, in execute_sql
for sql, params in self.as_sql():
File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 10
38, in as_sql
result.append(self.connection.ops.bulk_insert_sql(fields, placeholder_rows))

File "C:\Python34\lib\site-packages\mysql\connector\django\operations.py", lin
e 223, in bulk_insert_sql
return "VALUES " + ", ".join([items_sql] * num_values)
TypeError: can't multiply sequence by non-int of type 'tuple'


[manage.py]

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "swingqa.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)


[models.py]

from django.db import models
from django.utils import timezone


class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)

def publish(self):
self.published_date = timezone.now()
self.save()

def __str__(self):
return self.title


and for the migration file.. I looked inside the qablog > migrations folder and there are init and 0001_initial file.. init file is empty (it's just blank inside) and 0001_ initial file is like below

[0001_initial.py]

# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-11-10 07:56
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Post',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200)),
('text', models.TextField()),
('created_date', models.DateTimeField(default=django.utils.timezone.now)),
('published_date', models.DateTimeField(blank=True, null=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]


I'd really appreciate for some help :-)

Answer

found a solution to this question by myself :-)

I don't really know the reason but

if you change operations.py file in this path below C:\Python34\Lib\site-packages\mysql\connector\django

especially the part where def bulk_insert_sql function is,,from

 def bulk_insert_sql(self, fields, num_values):
        items_sql = "({0})".format(", ".join(["%s"] * len(fields)))
        return "VALUES " + ", ".join([items_sql] * num_values)

to ...

def bulk_insert_sql(self, fields, placeholder_rows):
    """
    Format the SQL for bulk insert
    """
    placeholder_rows_sql = (", ".join(row) for row in placeholder_rows)
    values_sql = ", ".join("(%s)" % sql for sql in placeholder_rows_sql)
    return "VALUES " + values_sql

there will be no errors in doing python manage.py migrate qablog command.