CustomUser 关系不存在:Django 迁移错误

问题描述

我对 Django 很陌生,在尝试创建自定义用户时遇到了问题。 我按照列出的所有步骤创建了一个,但因为我最初使用认的 User 模型,所以我删除了所有迁移和我的 postgres 数据库以从头开始(如果没有,我读它会导致问题)。

makemigrations 工作正常,但是当我想迁移时出现以下错误

create trigger testtrigger
on invoices
after insert
as
begin
    set nocount,xact_abort on;

    -- Check if *any* of the inserted products exceeds the current stock levels
    -- Taking into account the possibility of the same product occurring in multiple invoices
    -- You Could use similar logic to capture the products for which the stock level was exceeded
    -- However its very complex to only rollback those which are wrong.
    -- Really this should be your last line of defence,only used to ensure database integrity,so you should be checking stock levels before Now
    -- And therefore it shouldn't matter rolling everything back
    if exists (

        select 1
        from Inserted I
        inner join Products P on P.ProductId = I.productId
        where productID not in (1,2,3,4,5,6)
        group by P.Id,P.Stock
        having sum(I.Quantity) > P.Stock;

    ) begin
        -- If you want to do something in your calling code you need to throw an exception. Printing is only relevent while testing in SSMS.
        -- Throwing an exception will also roll back the transaction
        throw 51000,'Not enough Stock.',0;
    end;

end;

我的 docker 容器还输出以下内容

django.db.utils.ProgrammingError: relation "cyclabApp_customuser" does not exist.

Applying admin.0001_initial...Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py",line 84,in _execute
    return self.cursor.execute(sql,params)
psycopg2.errors.UndefinedTable: relation "cyclabApp_customuser" does not exist


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

Traceback (most recent call last):
  File "/app/manage.py",line 22,in <module>
    main()
  File "/app/manage.py",line 18,in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py",line 401,in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py",line 395,in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py",line 330,in run_from_argv
    self.execute(*args,**cmd_options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py",line 371,in execute
    output = self.handle(*args,**options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py",line 85,in wrapped
    res = handle_func(*args,**kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/migrate.py",line 243,in handle
    post_migrate_state = executor.migrate(
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py",line 117,in migrate
    state = self._migrate_all_forwards(state,plan,full_plan,fake=fake,fake_initial=fake_initial)
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py",line 147,in _migrate_all_forwards
    state = self.apply_migration(state,migration,line 229,in apply_migration
    migration_recorded = True
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py",line 115,in __exit__
    self.execute(sql)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py",line 142,in execute
    cursor.execute(sql,params)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py",line 98,in execute
    return super().execute(sql,line 66,in execute
    return self._execute_with_wrappers(sql,params,many=False,executor=self._execute)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py",line 75,in _execute_with_wrappers
    return executor(sql,many,context)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py",params)
  File "/usr/local/lib/python3.9/site-packages/django/db/utils.py",line 90,in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py",params)

我到处寻找类似的问题及其解决方案,但到目前为止没有运气试图解决这个问题......

在此先感谢您的帮助!!

解决方法

好的,我解决了。 显然,我缺少 UserManager 的模型。在我添加后工作。 :-)

class UserManager(BaseUserManager):
    def create_user(
            self,email,first_name,last_name,password=None,commit=True):
        """
        Creates and saves a User with the given email,first name,last name
        and password.
        """
        if not email:
            raise ValueError(_('Users must have an email address'))
        if not first_name:
            raise ValueError(_('Users must have a first name'))
        if not last_name:
            raise ValueError(_('Users must have a last name'))

        user = self.model(
            email=self.normalize_email(email),first_name=first_name,last_name=last_name,)

        user.set_password(password)
        if commit:
            user.save(using=self._db)
        return user