美文网首页
Django migrate 报错,通过fake 和 --fak

Django migrate 报错,通过fake 和 --fak

作者: 梨花菜 | 来源:发表于2020-01-07 18:44 被阅读0次

    mysql_exceptions.OperationalError: (1050, "Table 'api' already exists")

    因为这些表已经存在了,需要通过migrate --fake-initial 告诉Django已经存在

    [root@izwz9awyk38uq20rb3czmnz ~]# docker exec -it fasterrunner /bin/sh
    # python3 manage.py makemigrations --settings=FasterRunner.settings.pro fastrunner
    No changes detected in app 'fastrunner'
    # python3 manage.py migrate --settings=FasterRunner.settings.pro fastrunner
    Operations to perform:
      Apply all migrations: fastrunner
    Running migrations:
      Applying fastrunner.0001_initial...Traceback (most recent call last):
      File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 83, in _execute
        return self.cursor.execute(sql)
      File "/usr/local/lib/python3.6/dist-packages/django/db/backends/mysql/base.py", line 71, in execute
        return self.cursor.execute(query, args)
      File "/usr/local/lib/python3.6/dist-packages/MySQLdb/cursors.py", line 250, in execute
        self.errorhandler(self, exc, value)
      File "/usr/local/lib/python3.6/dist-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
        raise errorvalue
      File "/usr/local/lib/python3.6/dist-packages/MySQLdb/cursors.py", line 247, in execute
        res = self._query(query)
      File "/usr/local/lib/python3.6/dist-packages/MySQLdb/cursors.py", line 412, in _query
        rowcount = self._do_query(q)
      File "/usr/local/lib/python3.6/dist-packages/MySQLdb/cursors.py", line 375, in _do_query
        db.query(q)
      File "/usr/local/lib/python3.6/dist-packages/MySQLdb/connections.py", line 276, in query
        _mysql.connection.query(self, query)
    _mysql_exceptions.OperationalError: (1050, "Table 'api' already exists")
    
    

    初始化已存在的表 migrate --fake-initial

    # 指定Django的setting配置文件执行migrate 
     python3 manage.py migrate --fake-initial --settings=FasterRunner.settings.pro
    

    将会更新django_migrations

    image.png

    提示1054, "Unknown column 'name' in 'django_content_type'"

    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, djcelery, fastrunner, fastuser, sessions
    Running migrations:
      Applying contenttypes.0001_initial... FAKED
      Applying auth.0001_initial... FAKED
      Applying admin.0001_initial... FAKED
      Applying admin.0002_logentry_remove_auto_add... OK
      Applying admin.0003_logentry_add_action_flag_choices... OK
      Applying contenttypes.0002_remove_content_type_name...Traceback (most recent call last):
      File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 85, in _execute
        return self.cursor.execute(sql, params)
      File "/usr/local/lib/python3.6/dist-packages/django/db/backends/mysql/base.py", line 71, in execute
        return self.cursor.execute(query, args)
      File "/usr/local/lib/python3.6/dist-packages/MySQLdb/cursors.py", line 250, in execute
        self.errorhandler(self, exc, value)
      File "/usr/local/lib/python3.6/dist-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
        raise errorvalue
      File "/usr/local/lib/python3.6/dist-packages/MySQLdb/cursors.py", line 247, in execute
        res = self._query(query)
      File "/usr/local/lib/python3.6/dist-packages/MySQLdb/cursors.py", line 412, in _query
        rowcount = self._do_query(q)
      File "/usr/local/lib/python3.6/dist-packages/MySQLdb/cursors.py", line 375, in _do_query
        db.query(q)
      File "/usr/local/lib/python3.6/dist-packages/MySQLdb/connections.py", line 276, in query
        _mysql.connection.query(self, query)
    _mysql_exceptions.OperationalError: (1054, "Unknown column 'name' in 'django_content_type'")
    
    

    "Unknown column 'django_content_type.name' in 'field list

    因为django_content_type

    image.png
    需要django_content_type是空的
    需要加上项目的app_label和model
    image.png

    1054, "Unknown column 'django_content_type.name' in 'field list'"

      File "/usr/local/lib/python3.6/dist-packages/MySQLdb/connections.py", line 276, in query
        _mysql.connection.query(self, query)
    django.db.utils.OperationalError: (1054, "Unknown column 'django_content_type.name' in 'field list'")
    

    这是django_content_type中缺少新增加的表,补上就好

    image.png

    fake告诉Django已经在的表字段

    # python3 manage.py migrate --settings=FasterRunner.settings.pro fastrunner --fake
    Operations to perform:
      Apply all migrations: fastrunner
    Running migrations:
      No migrations to apply.
    

    最后执行新增加的表字段

    # python3 manage.py migrate --settings=FasterRunner.settings.pro fastrunner
    Operations to perform:
      Apply all migrations: fastrunner
    Running migrations:
      Applying fastrunner.0012_auto_20200107_1126... OK
    

    相关文章

      网友评论

          本文标题:Django migrate 报错,通过fake 和 --fak

          本文链接:https://www.haomeiwen.com/subject/pcaractx.html