简体   繁体   English

Django:表不存在

[英]Django : Table doesn't exist

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?我可以做些什么来获取该应用程序的表格?

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

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在models.py中评论你的模型
  2. go to step 3. BUT this time without --fake转到第 3 步。这次没有--fake

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注释掉主应用程序urls.py中的所有 URL

Then go ahead and run migrations:然后继续运行迁移:

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

The problem was alleviated by removing the () '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 得到了这个答案

I just ran migrations with the name of the app attached, for all the apps I had provisioned and that worked.我只是运行了附加应用程序名称的迁移,对于我配置的所有应用程序并且有效。

eg python3 manage.py makemigrations my_custom_app例如python3 manage.py makemigrations my_custom_app

After running for all of them I ran a migrate command to seal the deal.在为所有这些人运行后,我运行了一个迁移命令来达成交易。 python3 manage.py migrate . python3 manage.py migrate That was it.就是这样。 I'm still wondering why django behaves this way sometimes though.我仍然想知道为什么 django 有时会这样。

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 = False and 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_name does 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删除migrations文件夹内的所有迁移文件( init .py文件除外),然后makemigrations然后最后迁移
  2. above 2 answers以上 2 个回答
  3. this 这个

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会导致数据丢失

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

This will create the migration scripts again and will apply it to your database.这将再次创建迁移脚本并将其应用于您的数据库。

What solved my problem in situation when manage.py setmigration and then migrate to the SQL database is not working properly did is following:在 manage.py setmigration 然后迁移到 SQL 数据库无法正常工作的情况下解决我的问题的方法如下:

  1. Run command : python manage.py migrate --fake MyApp zero运行命令: python manage.py migrate --fake MyApp zero
  2. After that: python manage.py migrate MyApp之后: python manage.py migrate MyApp

And the problem that rises with connections.py and and after running correctly the migration command is solved!和connections.py 出现的问题,并且在正确运行迁移命令后解决了! At least for me.至少对于我来说。

I'm using Python (3.x), MySQL DB, Django (3.x), and I was in situation when I needed after some time of successfully creating tables in my database, that some error regarding connections.py raises.我正在使用 Python (3.x)、MySQL DB、Django (3.x),当我在我的数据库中成功创建表一段时间后需要时,我遇到了一些关于 connections.py 的错误。 But, above commands helped.但是,以上命令有帮助。 I hope they will help all those who are having these type of problems.我希望他们能帮助所有遇到此类问题的人。

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.虽然删除和重新创建表可能会解决问题,但值得检查您的数据库本身并查看model是否确实正确。 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.我在生产数据库上使用了python manage.py inspectdb命令,并将其与model进行了比较,并意识到在模型中它试图将数据插入到表 'test' 而不是 'Test' 中。 Hope that helps some of you in future.希望对你们中的一些人有帮助。

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.也许您正在将视图或查询加载到数据库,但您没有给 Django 足够的时间将模型迁移到数据库。 That's why the "table doesn't exist".这就是“表不存在”的原因。

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.第二种方法是清理以前的迁移,删除数据库并重新开始迁移过程。

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

如果 python manage.py migrate 仍然不起作用我的意思是当你这样做时它什么都不做你可以在之后从 django_migrations 表中删除应用程序的迁移

python manage.py migrate

I had a similar issue.我有一个类似的问题。

I had another python (with a class) file which need access to DB.我有另一个需要访问数据库的python(带类)文件。

For some reasons, when running 'makemigrations' this file was processed (I guess this is linked to some import chains).由于某些原因,当运行“makemigrations”时,这个文件被处理(我猜这与一些导入链有关)。

In this class, I had a method containing a default arg method(defaultModel=Model.get_default()) in the signature which was accessing to the default object in the DB (static method included in the Model class).在这个类中,我有一个方法在签名中包含一个默认的 arg method(defaultModel=Model.get_default()) ,该方法正在访问数据库中的默认对象(模型类中包含的静态方法)。

A the import time, this default arg was evaluated and as the table is not populated yet, it gives this error.导入时间,此默认参数已被评估,并且由于表尚未填充,因此会出现此错误。

So I just set None for the default args and asks for the default model object inside the method.所以我只是为默认参数设置None并要求方法内的默认模型对象。 This solved the issue.这解决了这个问题。

In this solution, your data will be removed.在此解决方案中,您的数据将被删除。 I removed the app and created the app again.我删除了该应用程序并再次创建了该应用程序。 I copied the app folder somewhere and delete the app folder from my project.我将 app 文件夹复制到某处并从我的项目中删除了 app 文件夹。 I commented on all lines in urls.py and files similar views.py and admin.py that use this app.我评论了 urls.py 中的所有行以及使用此应用程序的类似 views.py 和 admin.py 的文件。 also app name in settings.py.也是 settings.py 中的应用程序名称。

In mysql:在 mysql 中:

truncate django_migrations;
truncate django_admin_log;

Do this for all models in your app and change n.对应用程序中的所有模型执行此操作并更改 n。 n is app id. n 是应用程序 ID。

delete from auth_permission where content_type_id=n
delete from django_content_type where app_label='appname'

python manage.py startapp your_app_name

Then uncomment previous lines and restore files and run然后取消注释前几行并恢复文件并运行

python manage.py makemigrations
python manage.pt migrate

You can run你可以跑

python manage.py makemigrations --skip-checks python manage.py migrate--skip-checks python manage.py makemigrations --skip-checks python manage.py migrate--skip-checks

I faced the same problem, some of the above mentioned answers seemed not to work for me, but here's a simple 4 step solution:我遇到了同样的问题,上面提到的一些答案似乎对我不起作用,但这里有一个简单的 4 步解决方案:

    1) Delete the migrations files below __init__.py (don't delete __init__.py) in your specific app.
    2) python manage.py makemigrations AppName
    3) python manage.py migrate --fake AppName zero
    4) python manage.py migrate AppName

Hope these works for you.希望这些对你有用。

I faced the same issue earlier when I accidentally deleted my migrations folder in an app.当我不小心删除了应用程序中的迁移文件夹时,我之前遇到了同样的问题。 I was able to fix it by running manual makemigrations for that specific app.我能够通过为该特定应用程序运行手动makemigrations来修复它。

Here's the fix for Windows,这是 Windows 的修复程序,

py manage.py makemigrations <your_app_name>

py manage.py migrate

For other OS you need to replace py with python3 or python对于其他操作系统,您需要用python3python替换py

I hope this helped fix your issue!我希望这有助于解决您的问题!

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

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