Python Django:表不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27583744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:58:41  来源:igfitidea点击:

Django : Table doesn't exist

pythonmysqldjangodjango-models

提问by Vivek Ananthan

I dropped some table related to an app. and again tried the syncdb command

我删除了一些与应用程序相关的表。并再次尝试了 syncdb 命令

python manage.py syncdb

It shows error like

它显示错误,如

django.db.utils.ProgrammingError: (1146, "Table 'someapp.feed' doesn't exist")

models.py

模型.py

class feed(models.Model):
    user = models.ForeignKey(User,null=True,blank=True)
    feed_text = models.CharField(max_length=2000)
    date = models.CharField(max_length=30)
    upvote = models.IntegerField(default=0)
    downvote = models.IntegerField(default=0)

    def __str__(self):
        return feed.content

What I can do to get the tables for that app ?

我可以做些什么来获取该应用程序的表格?

采纳答案by doniyor

  1. drop tables (you already did),
  2. comment-out the model in model.py,
  3. and ..
  1. 删除表(你已经这样做了),
  2. 注释掉model.py中的模型,
  3. 和 ..

if django version >= 1.7:

如果 Django 版本 >= 1.7:

python manage.py makemigrations
python manage.py migrate --fake

else

别的

python manage.py schemamigration someapp --auto
python manage.py migrate someapp --fake
  1. comment-in your model in models.py
  2. go to step 3. BUTthis time without --fake
  1. 在models.py中评论你的模型
  2. 转到第 3 步。这次没有--fake

回答by Keith Yong

For those that may still be having trouble (like me), try this out:

对于那些可能仍然有问题的人(像我一样),试试这个:

Comment out all the URL's in the main app's urls.py

注释掉主应用程序中的所有 URL urls.py

Then go ahead and run migrations:

然后继续运行迁移:

$ ./manage.py makemigrations
$ ./manage.py migrate

The problem was alleviated by removing the ()'s

通过删除()'s缓解了该问题

    solved_time = models.DateTimeField('solved time', default=timezone.now())

to

    solved_time = models.DateTimeField('solved time', default=timezone.now)

I got this answer from reddit

我从 reddit 得到了这个答案

回答by coda

none of the above solutions worked for me, I finally solved by

以上解决方案都不适合我,我终于解决了

sudo systemctl stop mysql.service

sudo apt-get purge mysql-server

sudo apt-get install mysql-server

sudo systemctl stop mysql.service

In my case the code that I pulled had managed= Falseand I wanted the tables to be maintained by Django.

在我的情况下,我提取的代码已经管理= False,我希望这些表由 Django 维护。

But when I did makemigrations the custom tables were not being detected or I was getting the error that the app_name.Table_namedoes not exist

但是当我进行 makemigrations 时,没有检测到自定义表,或者我收到了app_name.Table_name不存在的错误

I tried the following:

我尝试了以下方法:

  1. delete all the migration files inside the migrations folder (except init.py file) and then makemigrations then finally migrate
  2. above 2 answers
  3. this
  1. 删除migrations文件夹内的所有迁移文件(init.py文件除外),然后makemigrations然后最后迁移
  2. 以上 2 个回答
  3. 这个

PS: This solution is only feasible if backup is present or data is not important or you are just started creating the tables, as purging mysql will lead to loss of data

PS:此方案只适用于有备份或数据不重要或刚开始建表的情况,因为清除mysql会导致数据丢失

回答by echohabz

I had this issue where I was playing with same database structure in production vs development. While dropping and recreating tables will probably resolve the issue, its worth checking your database itself and see if the model is actually correct. For myself I created the development database incorrectly with the table names all in lowercase while in production the first letter of tables were capitalized. I used the python manage.py inspectdb command on production db, and compared it to the model and realized that in the model it was trying to insert data to table 'test' instead of 'Test' for example. Hope that helps some of you in future.

我在生产与开发中使用相同的数据库结构时遇到了这个问题。虽然删除和重新创建表可能会解决问题,但值得检查您的数据库本身并查看模型是否确实正确。对于我自己,我错误地创建了开发数据库,​​表名全部小写,而在生产中,表的第一个字母大写。我在生产数据库上使用了 python manage.py inspectdb 命令,并将其与模型进行了比较,并意识到在模型中它试图将数据插入到表 'test' 而不是 'Test' 中。希望对你们中的一些人有帮助。

回答by Jorge Cardenas

I have to face same issue and there are a couple of approaches, but the one I think is the most probable one.

我必须面对同样的问题,有几种方法,但我认为最有可能的一种。

Maybe you are loading views or queries to database but you haven′t granted enough time for Django to migrate the models to DB. That's why the "table doesn't exist".

也许您正在将视图或查询加载到数据库,但您没有给 Django 足够的时间将模型迁移到数据库。这就是“表不存在”的原因。

Make sure you use this sort of initialization in you view's code:

确保在视图代码中使用这种初始化:

Class RegisterForm(forms.Form):

类 RegisterForm(forms.Form):

  def __init__(self, *args, **kwargs):
    super(RegisterForm, self).__init__(*args, **kwargs)

A second approach is you clean previous migrations, delete the database and start over the migration process.

第二种方法是清理以前的迁移,删除数据库并重新开始迁移过程。

回答by goose

This is linked to the migration data in the scripts inside the project not matching with the migration scripts in the database as far as I could tell. I solved this by the following steps :

据我所知,这与项目内脚本中的迁移数据有关,但与数据库中的迁移脚本不匹配。我通过以下步骤解决了这个问题:

  1. Delete all the migration scripts under migration folder except __ini__
  2. Make sure that the model.py contains the same structure as the table in the database and managed=True
  3. Remove all Django Created tables like auth_user,... etc
  4. Run the following code
  1. 删除迁移文件夹下的所有迁移脚本,除了 __ini__
  2. 确保 model.py 包含与数据库中的表相同的结构,并且 managed=True
  3. 删除所有 Django 创建的表,如 auth_user 等
  4. 运行以下代码
$ ./manage.py makemigrations
$ ./manage.py migrate

This will create the migration scripts again and will apply it to your database.

这将再次创建迁移脚本并将其应用于您的数据库。

回答by Shedrack

I tried all of the above tricks and never worked for me. I commented on all imports and URLs that called a particular Table

我尝试了上述所有技巧,但从未对我有用。我评论了所有调用特定表的导入和 URL