简体   繁体   English

Django:'字段列表'中的未知列'last_login'

[英]Django: Unknown column 'last_login' in 'field list'

I did look through other topics but I couldn't find anything useful or that would help me. 我确实看过其他主题,但我找不到任何有用的东西或者对我有帮助。 All I did was doing python manage.py inspectdb > models.py and then edit the file a bit, the user model and then did migrate. 我所做的只是做python manage.py inspectdb > models.py ,然后稍微编辑文件,用户模型,然后进行迁移。

Now when I try to create a super user I get this error 现在,当我尝试创建超级用户时,我收到此错误

D:\Programming\Web\blaine county\website>python manage.py createsuperuser --username=andrei --email=andreigames9@gmail.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
Password:
Password (again):
Traceback (most recent call last):
  File "D:\Programming\Python\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "D:\Programming\Python\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "D:\Programming\Python\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
    res = self._query(query)
  File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
    db.query(q)
  File "D:\Programming\Python\lib\site-packages\MySQLdb\connections.py", line 277, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1054, "Unknown column 'last_login' in 'field list'")

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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "D:\Programming\Python\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "D:\Programming\Python\lib\site-packages\django\core\management\__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Programming\Python\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\Programming\Python\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 59, in execute
    return super().execute(*args, **options)
  File "D:\Programming\Python\lib\site-packages\django\core\management\base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "D:\Programming\Python\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 179, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
  File "D:\Programming\Python\lib\site-packages\django\contrib\auth\models.py", line 161, in create_superuser
    return self._create_user(username, email, password, **extra_fields)
  File "D:\Programming\Python\lib\site-packages\django\contrib\auth\models.py", line 144, in _create_user
    user.save(using=self._db)
  File "D:\Programming\Python\lib\site-packages\django\contrib\auth\base_user.py", line 73, in save
    super().save(*args, **kwargs)
  File "D:\Programming\Python\lib\site-packages\django\db\models\base.py", line 729, in save
    force_update=force_update, update_fields=update_fields)
  File "D:\Programming\Python\lib\site-packages\django\db\models\base.py", line 759, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "D:\Programming\Python\lib\site-packages\django\db\models\base.py", line 842, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "D:\Programming\Python\lib\site-packages\django\db\models\base.py", line 880, in _do_insert
    using=using, raw=raw)
  File "D:\Programming\Python\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "D:\Programming\Python\lib\site-packages\django\db\models\query.py", line 1125, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "D:\Programming\Python\lib\site-packages\django\db\models\sql\compiler.py", line 1280, in execute_sql
    cursor.execute(sql, params)
  File "D:\Programming\Python\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "D:\Programming\Python\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "D:\Programming\Python\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "D:\Programming\Python\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "D:\Programming\Python\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "D:\Programming\Python\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "D:\Programming\Python\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "D:\Programming\Python\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
    res = self._query(query)
  File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
    db.query(q)
  File "D:\Programming\Python\lib\site-packages\MySQLdb\connections.py", line 277, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1054, "Unknown column 'last_login' in 'field list'")

This is my models.py https://pastebin.com/5sEY7t5H 这是我的models.py https://pastebin.com/5sEY7t5H

The thing is that I had a legacy database and I tried to connect the app to it. 问题是我有一个遗留数据库,我试图将应用程序连接到它。

There are a couple of things you need to consider when using user models and legacy databases. 使用用户模型和旧数据库时,需要考虑几件事情。

In Django there are four User options built it and what you're trying to do here is not any of them. 在Django中有四个User选项构建它,你在这里尝试做的不是它们中的任何一个。

First option is to use default User model, second option is to use AbstractUser model, third options is AbstractBaseUser and fourth option is linking back from a related model. 第一个选项是使用默认的User模型,第二个选项是使用AbstractUser模型,第三个选项是AbstractBaseUser ,第四个选项是从相关模型链接回来。

What you are trying to do here is to use AbstractUser option, but this doesn't work like that. 你在这里尝试做的是使用AbstractUser选项,但这不是那样的。 AbstractUser options uses all of the field from base User models + additional fields defined in the model. AbstractUser选项使用基本User模型中的所有字段+模型中定义的其他字段。

AbstractBaseUser is bare-bones option and uses three fields from default User model: password , last_login , is_active and whatever other fields you define in the model. AbstractBaseUser是bare-bones选项,它使用默认User模型中的三个字段: passwordlast_loginis_active以及您在模型中定义的任何其他字段。

To make it work, you need to run makemigrations and migrate and this will create User model with fields such as last_login plus all of your additional fields. 要使其工作,您需要运行makemigrations并进行migrate ,这将创建User模型,其中包含last_login等字段以及所有其他字段。 But you cannot do that, because you're using legacy database with managed = False . 但是你不能这样做,因为你使用的是managed = False旧数据库。 It means: 它的意思是:

If False, no database table creation or deletion operations will be performed for this model. 如果为False,则不会对此模型执行数据库表创建或删除操作。 This is useful if the model represents an existing table or a database view that has been created by some other means. 如果模型表示通过其他方式创建的现有表或数据库视图,则此选项非常有用。

This means that with legacy database and managed = False option, you won't be able to make migrations and migrate, which consequently means that none of the Django options for User models are possible, because you'll always be missing one or another field. 这意味着使用旧数据库和managed = False选项,您将无法进行迁移和迁移,这意味着User模型的Django选项都不可能,因为您将始终缺少一个或另一个字段。

If you're going to use legacy database and your own user model, then you need to dive deep into Django and rewrite (overwrite) at least authentication, login and logout functionalities, because they just won't work with your Users model. 如果您要使用旧数据库和您自己的用户模型,那么您需要深入研究Django并重写(覆盖)至少身份验证,登录和注销功能,因为它们不适用于您的Users模型。 You can't just copy database that was created with EntityFramework (I assume) and expect it will work with Django. 您不能只复制使用EntityFramework创建的数据库(我假设)并期望它可以与Django一起使用。

You have more or less three sane options: 你有或多或少三个理智的选择:

  1. Ditch Users model from legacy database (you don't have to delete it, just change it from AbstractBase to models.Model ) and setup your own in Django and then use legacy database just for storing data. 来自旧数据库的Ditch Users模型(您不必删除它,只需将其从AbstractBase更改为models.Model )并在Django中设置您自己的数据,然后仅使用旧数据库来存储数据。
  2. If you are allowed to do "whatever" with legacy database, switch tables you need to managed = True , delete fields that clash with Django User model in your Users model, make migrations and migrate. 如果你被允许做“什么”与传统的数据库,交换机需要的表managed = True ,删除Django的冲突域User模型中的Users模型,使迁移和迁移。
  3. Create new database and copy data from legacy database to new database. 创建新数据库并将数据从旧数据库复制到新数据库。

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

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