0

我正在尝试在我的 Flask 应用程序中进行注册。我已经创建了两个角色(“管理员”和“用户”)和一个管理员(我),没有任何问题。但是,当我尝试通过注册页面注册新用户时,出现以下错误:

user.role_id may not be NULL

模型(标准快速入门):类 Role(db.Model, RoleMixin):name = CharField(unique=True) description = TextField(null=True)

class User(db.Model, UserMixin):
    email = TextField()
    password = TextField()
    active = BooleanField(default=True)
    confirmed_at = DateTimeField(null=True)

class UserRoles(db.Model):
    # Because peewee does not come with built-in many-to-many
    # relationships, we need this intermediary class to link
    # user to roles.
    user = ForeignKeyField(User, related_name='roles')
    role = ForeignKeyField(Role, related_name='users')
    name = property(lambda self: self.role.name)
    description = property(lambda self: self.role.description)

我真的不明白这是从哪里来的。首先,您不需要为新用户分配角色,其次,由于 'role' 是外键,因此获取用户角色 id 的正确方法是user.role.id代替user.role_id.

4

1 回答 1

0

看起来您可能role在 User 表上的某个时间点有一个外键,但也许您删除了它?如果您不再使用它,您可能需要更改您的表以删除该列。

于 2014-02-07T15:42:04.387 回答