Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我会得到;“未找到表格”;即使我能看到它被创造出来?_Python_Sql_Django - Fatal编程技术网

Python 为什么我会得到;“未找到表格”;即使我能看到它被创造出来?

Python 为什么我会得到;“未找到表格”;即使我能看到它被创造出来?,python,sql,django,Python,Sql,Django,在阅读Django关于身份验证的文档之前,我首先创建了用户模型,因此我将所有属性都放在同一个模型中。所以,后来我尝试将其分为用户和用户配置文件。但当我运行填充脚本时,它说找不到用户配置文件表,尽管我看到了创建它的SQL 这两个类连接到我导入的用户模型 from django.contrib.auth.models import User class UserProfile(models.Model): user = models.OneToOneField(User)

在阅读Django关于身份验证的文档之前,我首先创建了用户模型,因此我将所有属性都放在同一个模型中。所以,后来我尝试将其分为用户和用户配置文件。但当我运行填充脚本时,它说找不到用户配置文件表,尽管我看到了创建它的SQL

这两个类连接到我导入的用户模型

from django.contrib.auth.models import User

class UserProfile(models.Model):
        user = models.OneToOneField(User)
        profilepic = models.ImageField(blank=True)
        city = models.ForeignKey(City)
        slug = models.SlugField(unique=True)

        def save(self, *args, **kwargs):
            @property
            def avg_rating(self):
                return self.userrating_set.all().aggregate(Avg('rating'))['rating__avg']

    class UserRating(models.Model):
        user = models.ForeignKey(User)
        comment = models.CharField(max_length=500)
        for_username = models.CharField(max_length=128)
        rating = models.IntegerField(default=5)

        def __unicode__(self):
            return unicode(self.rating)
这是人口脚本中存在问题的部分:

new_user = User.objects.get_or_create(username=username, email=email)[0]
#new_user.profilepic = profile_picture
new_user.firstname = first_name
new_user.secondname = last_name
new_user.save()

new_user_profile = UserProfile.objects.get_or_create(user=new_user, city=created_city)
new_user_profile.slug = username
new_user_profile.save()
这是我在运行脚本时遇到的错误:

Traceback (most recent call last):
  File "C:\Users\bnbih\app_name\populationScript.py", line 108, in <module>
    populate()
  File "C:\Users\bnbih\app_name\populationScript.py", line 101, in populate
    new_user_profile = UserProfile.objects.get_or_create()
  File "C:\python27\lib\site-packages\django\db\models\manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\python27\lib\site-packages\django\db\models\query.py", line 422, in get_or_create
    return self.get(**lookup), False
  File "C:\python27\lib\site-packages\django\db\models\query.py", line 351, in get
    num = len(clone)
  File "C:\python27\lib\site-packages\django\db\models\query.py", line 122, in __len__
    self._fetch_all()
  File "C:\python27\lib\site-packages\django\db\models\query.py", line 966, in _fetch_all
    self._result_cache = list(self.iterator())
  File "C:\python27\lib\site-packages\django\db\models\query.py", line 265, in iterator
    for row in compiler.results_iter():
  File "C:\python27\lib\site-packages\django\db\models\sql\compiler.py", line 700, in results_iter
    for rows in self.execute_sql(MULTI):
  File "C:\python27\lib\site-packages\django\db\models\sql\compiler.py", line 786, in execute_sql
    [Finished in 0.8s with exit code 1]cursor.execute(sql, params)
  File "C:\python27\lib\site-packages\django\db\backends\utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\python27\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\python27\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\python27\lib\site-packages\django\db\backends\sqlite3\base.py", line 485, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: mainapp_userprofile
回溯(最近一次呼叫最后一次):
文件“C:\Users\bnbih\app\u name\populationScript.py”,第108行,在
填充()
文件“C:\Users\bnbih\app\u name\populationScript.py”,第101行,在populate中
new_user_profile=UserProfile.objects.get_或_create()
文件“C:\python27\lib\site packages\django\db\models\manager.py”,第92行,在manager\u方法中
返回getattr(self.get_queryset(),name)(*args,**kwargs)
文件“C:\python27\lib\site packages\django\db\models\query.py”,第422行,在get\u或\u create中
返回self.get(**查找),False
文件“C:\python27\lib\site packages\django\db\models\query.py”,第351行,在get中
num=len(克隆)
文件“C:\python27\lib\site packages\django\db\models\query.py”,第122行,在__
self._fetch_all()
文件“C:\python27\lib\site packages\django\db\models\query.py”,第966行,在_fetch\u all中
self.\u result\u cache=list(self.iterator())
迭代器中第265行的文件“C:\python27\lib\site packages\django\db\models\query.py”
对于编译器.results\u iter()中的行:
文件“C:\python27\lib\site packages\django\db\models\sql\compiler.py”,第700行,在results\u iter中
对于self.execute_sql(多)中的行:
文件“C:\python27\lib\site packages\django\db\models\sql\compiler.py”,第786行,在execute\U sql中
[在0.8s内完成,退出代码为1]游标。执行(sql,参数)
文件“C:\python27\lib\site packages\django\db\backends\utils.py”,执行中的第81行
返回super(CursorDebugWrapper,self).execute(sql,params)
文件“C:\python27\lib\site packages\django\db\backends\utils.py”,第65行,执行
返回self.cursor.execute(sql,params)
文件“C:\python27\lib\site packages\django\db\utils.py”,第94行,在退出时__
6.重新播放(dj_exc_类型、dj_exc_值、回溯)
文件“C:\python27\lib\site packages\django\db\backends\utils.py”,第65行,执行
返回self.cursor.execute(sql,params)
文件“C:\python27\lib\site packages\django\db\backends\sqlite3\base.py”,第485行,在execute中
返回Database.Cursor.execute(self、query、params)
django.db.utils.OperationalError:没有这样的表:mainapp\u userprofile
Django只是向您显示将运行的内容,它不应用任何更改,您需要运行

打印命名迁移的SQL。这需要一个活动的数据库连接,它将使用该连接解析约束名称;这意味着您必须针对希望稍后应用SQL的数据库副本生成SQL


您在哪里看到“SQL创建它”?如果运行
makemigrations
/
migrate
,是否显示任何内容?当我运行SQLmigrate时,我可以看到:创建表“mainapp\u userprofile”@Sei.M.-为什么要删除迁移?删除它们绝对不是一个好主意,您可能需要运行
migrate mainapp XXXX
,其中
XXXX
是最新迁移的前缀号,(可能是
migrate mainapp 0
先删除本地数据库,然后重新创建)