[英]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):我进行了迁移,发生了两件意想不到的事情(我是菜鸟,这就是他们出乎意料的原因):
db_table
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:所以,我的问题有两个:
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.