我有角色模型,它在我的应用程序中的 FastAPI+Tortoise 中与用户模型作为 FK 连接
class UserRoles(Model):
id = UUIDField(pk=True, default=uuid.uuid4)
name = CharField(max_length=255, description='Наименование', unique=True)
class Meta:
table = 'roles'
和用户模型
class UsersModel(Model, ObjChangesDatetimeMixin):
id = UUIDField(pk=True, default=uuid.uuid4)
name = CharField(max_length=255, description='ФИО')
email = CharField(max_length=255, unique=True)
password = CharField(max_length=255)
is_active = BooleanField(default=True)
role = ForeignKeyField('models.UserRoles', related_name='users')
class Meta:
table = 'users'
迁移后,我在我的 sql 文件中得到了这个:
ALTER TABLE "users" ADD CONSTRAINT "fk_users_roles_2657b48c" FOREIGN KEY ("role_id") REFERENCES "roles" ("id") ON DELETE CASCADE;
但是导师说我应该使用正确的约束名称“users__role_id_fk”创建。我需要做的是获取 sql 等记录:
ALTER TABLE "users" ADD CONSTRAINT "users__role_id_fk" FOREIGN KEY ("role_id") REFERENCES "roles" ("id") ON DELETE CASCADE;