简体   繁体   English

运行迁移命令Django时出错

[英]Error when running migrate command Django

I am trying to deploy my first web project on a server. 我正在尝试在服务器上部署我的第一个Web项目。 I am getting this error when running migrate and makemigrations: 运行迁移和makemigrations时出现此错误:

ProgrammingError (1146, "Table '<user>$<dbname>.<table_name>'' doesn't exist")

Everywhere it says that you should run manage.py syncdb but this is obsolete , also when running manage.py --run-syncdb the same error is given. 到处都说您应该运行manage.py syncdb但这已经过时了,而且在运行manage.py --run-syncdb时,也会出现相同的错误。

models 楷模

class Book(models.Model):

    name = models.CharField(max_length=350)
    author = models.CharField(max_length=350)
    category = models.CharField(max_length=200)

    def __str__(self):
        return self.name

snipppets from settings 设置中的摘录

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '<your_username>$<your_database_name>',
        'USER': '<your_username>',
        'PASSWORD': '<your_mysql_password>',
        'HOST': '<your_mysql_hostname>',
    }
} #Everything is replaced with the correct credentials

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main_app',
    'accounts',
]

The DB is empty on the server. 服务器上的数据库为空。 On my PC (when I developed it locally) it worked, it created the table for me when it didn't exist in the DB. 在我的PC上(当我在本地开发它时),它可以工作,当数据库中不存在该表时,它会为我创建表。

I am using Django 1.11 and Mysql 5.6.27. 我正在使用Django 1.11和Mysql 5.6.27。

I tried other answers but did not help me. 我尝试了其他答案,但没有帮助我。 Can you please suggests what can I do to solve this issue? 您能建议我该怎么做才能解决这个问题?

You should use migrations mechanism: https://docs.djangoproject.com/en/1.11/topics/migrations/ 您应该使用迁移机制: https : //docs.djangoproject.com/en/1.11/topics/migrations/

./manage.py makemigrations # this creates migrations files
./manage.py migrate # this applies migrations

You don't need to use syncdb. 您不需要使用syncdb。
for the first time you creating your Django project in order to creating pre-installed apps (such as admin interface) database in django you need to use : 第一次创建Django项目以便在Django中创建预安装的应用程序(例如管理界面)数据库时,您需要使用:
python manage.py migrate

it creates all necessary database tables and relations. 它创建所有必要的数据库表和关系。
but every time you make changes to your app like creating new columns in your models, you can use: 但是每次您对应用程序进行更改(例如在模型中创建新列)时,都可以使用:
python manage.py makemigrations [your app name]
the result will be something like below : 结果将如下所示:
(env35) someone@shell:~/bookstore$ python manage.py makemigrations books Migrations for 'books': 0005_publisher_family.py: - Add field family to publisher
and finally you run: 最后,您运行:

python manage.py migrate

to Synchronizes the database state with the current set of models and migrations. 使数据库状态与当前模型和迁移集同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM