有mysql迁移的麻烦(不能将序列乘以'tuple'类型的非int)

时间:2016-11-10 09:16:52

标签: python mysql django

我对迁移感到困惑。我第一次尝试了

  

python manage.py migrate qablog

并没有奏效。所以我试过

python manage.py migrate qablog --fake-initial

并且也没有工作..所以我再次搜索,有些人正在谈论更改settings.py所以我改变了一些事情,如将USE_TZ更改为false ..

  

python manage.py makemigrations qablog

虽然工作得很好......任何人都可以帮助我吗? .. python版本:3.4.4 mysql服务器版本:5.7 django版本:1.10.3

错误日志如下所示:

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

和迁移文件..我查看了qablog&gt;迁移文件夹,还有 init 和0001_initial文件.. init 文件为空(内部空白),0001_初始文件如下所示

[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)),
            ],
        ),
    ]

我非常感谢你的帮助: - )

1 个答案:

答案 0 :(得分:4)

我自己找到了这个问题的解决方案: - )

我真的不知道原因,但

如果您在下面的此路径中更改operations.py文件 C:\ Python34 \ LIB \站点包\ MySQL的\连接器\ django的

特别是def bulk_insert_sql函数所在的部分,来自

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

到...

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

执行python manage.py migrate qablog命令时不会出错。