简体   繁体   English

将 db_table 添加到 model 添加了 last_login 字段

[英]Adding db_table to model added a last_login field

Background: I started a project with a custom User model.背景:我使用自定义用户 model 开始了一个项目。 However, noob that I am, I was unaware of the AbstractBaseUser class.但是,我是菜鸟,我不知道 AbstractBaseUser class。 So I just wrote my own.所以我只写了我自己的。 The app has been deployed to prod and working fine.该应用程序已部署到 prod 并且工作正常。 But now I want to switch to using AbstractBaseUser so I can take advantage of some of the built-in Django utilities (like the pre-made password resetting process).但现在我想切换到使用 AbstractBaseUser,这样我就可以利用一些内置的 Django 实用程序(如预制密码重置过程)。 I had done this with a different app and it worked fine.我用不同的应用程序做到了这一点,它运行良好。 But that one wasn't in prod while I made the change.但是当我做出改变时,那个并没有出现。 Because this one is, I needed to keep the old user table while I made the changes with a copy of it.因为这是一个,所以我需要保留旧的用户表,同时使用它的副本进行更改。 So my first step was to add db_table = test_users to my old user model, so as to keep the prod app running with an unchanged table.所以我的第一步是将db_table = test_users添加到我的旧用户 model 中,以保持 prod 应用程序使用未更改的表运行。 I ran the migration, and two unexpected things happened (I'm a noob, and that's why they were unexpected):我进行了迁移,发生了两件意想不到的事情(我是菜鸟,这就是他们出乎意料的原因):

  • The old user table was renamed.旧的用户表已重命名。 I thought a new table would be created.我以为会创建一个新表。 No problem, I quickly copied the new table and named the copy with the old table's name so the prod app could still find its users没问题,我快速复制了新表并用旧表的名称命名了副本,这样产品应用程序仍然可以找到它的用户
  • A column last_login was added.添加了 last_login 列。 Why??为什么?? Here's my model, with the added db_table这是我的 model,添加了db_table

    class User(models.Model):
        first_name = models.CharField(max_length=255)
        last_name = models.CharField(max_length=255)
        email = models.CharField(max_length=255)
        password = models.CharField(max_length=255)
        created_at = models.DateTimeField(auto_now_add=True)
        updated_at = models.DateTimeField(auto_now=True)
        client_id = models.IntegerField(blank=True, null=True)
        is_admin = models.BooleanField(default=False)
        is_super = models.BooleanField(default=False)
        is_active = models.BooleanField(default=True)
    
        class Meta:
            db_table = "test_users"

The big problem with this is that when I change to AbstractBaseUser and run the migration, I get an error.最大的问题是当我更改为AbstractBaseUser并运行迁移时,我得到一个错误。 Looking at the migration file I see that this change creates a migration that all it tries to do is to add last_login to the table.查看迁移文件,我发现此更改创建了一个迁移,它所要做的就是将last_login添加到表中。 So, of course, the error I get is "Duplicate column name 'last_login'"所以,当然,我得到的错误是“重复的列名'last_login'”

So, my question is two-fold:所以,我的问题有两个:

  1. Why was that column added in the first migration?为什么在第一次迁移中添加了该列?
  2. If I just run migrate --fake and keep going, will it have unintended consequences?如果我只是运行migrate --fake并继续运行,它会产生意想不到的后果吗? I thought this could be a good solution, given that the migration file shows nothing else is being done, and if the field already exists, then no harm done?我认为这可能是一个很好的解决方案,因为迁移文件显示没有做任何其他事情,并且如果该字段已经存在,那么不会造成伤害吗?

Maybe because you've changed the parent class django automatically change all the migrations that connected to your user class也许是因为您更改了父 class django 自动更改连接到您的用户 class 的所有迁移

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

相关问题 Django:'字段列表'中的未知列'last_login' - Django: Unknown column 'last_login' in 'field list' 无法更新Django内置用户的last_login字段 - Unable to Update last_login field of Django in-built user 如何更改自定义Django用户创建的数据库列last_login的名称? - How to change the name of db column last_login created by custom Django user? 获取所有用户的last_login吗? - Get last_login for all users? 在 Django 中显示加入日期和 last_login - Display join date and last_login in Django 在 Django Rest 框架中使用 Tokenauthentication 进行身份验证时,last_login 字段未更新 - last_login field is not updated when authenticating using Tokenauthentication in Django Rest Framework 从Django 1.6(南方)升级到1.8不会修改用户表上的“last_login” - Upgrading from Django 1.6 (with south) to 1.8 doesn't modify 'last_login' on the user table 将django json夹具与手动设置了Meta db_table的模型一起使用 - Using django json fixture with model having Meta db_table set manually 更改Django / Postgres db_table名称 - Changing Django/Postgres db_table name 在迁移后更改 Django 上的 db_table 不起作用,并且添加用户 (AbstractUser) 外键会破坏 __str__() - Changing db_table on Django doesn't work after migrations, and adding a user (AbstractUser) foreign key breaks __str__()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM