首页 > 解决方案 > 克隆项目失败后运行迁移的 Django

问题描述

我刚刚从 git 存储库中克隆了我的项目。安装所有依赖项和虚拟环境后,我继续通过运行迁移来设置数据库,如下所示:

python3.6 manage.py makemigrations
python3.6 manage.py makemigrations api
python3.6 manage.py migrate

但最后一个命令失败:

python3.6 manage.py migrate
Operations to perform:
  Apply all migrations: account, admin, api, auth, authtoken, contenttypes, sessions, sites, socialaccount
Running migrations:
  Applying account.0001_initial...Traceback (most recent call last):
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql)
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 394, in execute
    return Database.Cursor.execute(self, query)
sqlite3.OperationalError: no such table: users_user

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 233, in handle
    fake_initial=fake_initial,
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 247, in apply_migration
    migration_recorded = True
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 35, in __exit__
    self.connection.check_constraints()
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 354, in check_constraints
    column_name, referenced_column_name,
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql)
  File "/Users/bigweld/Desktop/webdev/projects/koffeecultor-api/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 394, in execute
    return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: no such table: users_user

这是我的模型:

kofiapi/api/products/models.py

from django.db import models
import uuid

class Product(models.Model):

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

    sku = models.CharField(unique=True, blank=False, max_length=13, help_text="Enter Product Stock Keeping Unit")
    slug = models.SlugField(blank=True, unique=True)
    name = models.CharField(blank=False, max_length=200, help_text="Enter Product Name")

    short_description = models.CharField(max_length=350, help_text="Enter Product Short Description (350 characters)")
    description = models.TextField(help_text="Enter Product Description")

    image = models.ImageField(null=True, blank=True)

    cost = models.FloatField(blank=False, help_text="Enter Product Cost")


    def __str__(self):
        return self.name

kofiapi/api/users/models.py

import uuid
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.translation import ugettext_lazy as _
from django.conf import settings

class User(AbstractUser):

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

    username = models.CharField(max_length=120, blank=True, null=True)
    email    = models.EmailField(_('email address'), unique=True)

    USERNAME_FIELD  = 'email'
    REQUIRED_FIELDS = ['username', 'first_name', 'last_name']

    def __str__(self):
        return "{}".format(self.email)


class UserProfile(models.Model):

    user = models.OneToOneField(settings.AUTH_USER_MODEL, 
                                on_delete=models.CASCADE, 
                                related_name='profile')

    dob = models.DateField()
    phone = models.CharField(max_length=15)
    active = models.BooleanField(default=True)
    receive_newsletter = models.BooleanField(default=False, blank=True)

以及我将它们全部导入的文件:

kofiapi/api/models.py

from kofiapi.api.users.models import User, UserProfile
from kofiapi.api.products.models import Product

这是我的项目结构:

.
├── db.sqlite3
├── kofiapi
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   └── admin.cpython-36.pyc
│   ├── admin.py
│   ├── api
│   │   ├── __pycache__
│   │   │   ├── models.cpython-36.pyc
│   │   │   ├── pagination.cpython-36.pyc
│   │   │   ├── permissions.cpython-36.pyc
│   │   │   └── urls.cpython-36.pyc
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   │       ├── 0001_initial.cpython-36.pyc
│   │   │       └── __init__.cpython-36.pyc
│   │   ├── models.py
│   │   ├── orders
│   │   │   ├── __init__.py
│   │   │   ├── __pycache__
│   │   │   │   ├── __init__.cpython-36.pyc
│   │   │   │   └── models.cpython-36.pyc
│   │   │   ├── models.py
│   │   │   ├── serializers.py
│   │   │   └── views.py
│   │   ├── pagination.py
│   │   ├── permissions.py
│   │   ├── products
│   │   │   ├── __init__.py
│   │   │   ├── __pycache__
│   │   │   │   ├── __init__.cpython-36.pyc
│   │   │   │   ├── models.cpython-36.pyc
│   │   │   │   ├── serializers.cpython-36.pyc
│   │   │   │   └── views.cpython-36.pyc
│   │   │   ├── models.py
│   │   │   ├── serializers.py
│   │   │   └── views.py
│   │   ├── urls.py
│   │   └── users
│   │       ├── __init__.py
│   │       ├── __pycache__
│   │       │   ├── __init__.cpython-36.pyc
│   │       │   ├── models.cpython-36.pyc
│   │       │   ├── serializers.cpython-36.pyc
│   │       │   └── views.cpython-36.pyc
│   │       ├── models.py
│   │       ├── serializers.py
│   │       └── views.py
│   ├── apps.py
│   ├── migrations
│   └── tests.py
├── kofistore
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── settings.cpython-36.pyc
│   │   └── urls.cpython-36.pyc
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── readme.txt
├── requirements.txt

我已经尝试过:

与在我的笔记本电脑上正常运行的原始项目相比,没有任何变化。

谢谢

标签: djangopython-3.xdjango-rest-frameworkdjango-migrations

解决方案


推荐阅读