DynamoDB table.get_item() 被执行了两次

DynamoDB table.get_item() get executed twice

我正在努力创建几个 tables 的 DAO 层。 由于有许多方法将在不同的 DAO 中使用,我想为所有这些方法使用父 class 并实现一些通用方法。我将 table_name__name__ 属性传递给父 class 以便它将创建子 class 的记录器并创建正确的 table 变量。

下面是我的做法。

class Parent(object):    
    def __init__(self, class_name, table_name):
        ddb = boto3.resource("dynamodb", region_name=constants.AWS_REGION)
        self.table_name = table_name
        self.table = ddb.Table(table_name)
        self.logger = logging.getLogger(class_name)

    def get_record(self, **kwargs):
        try :
            self.logger.info(f"self type => {type(self)}")
            record = self.table.get_item(**kwargs)
            self.logger.info(f"record => {record}")
            record = record['Item']
            if record == {}:
                raise NoRecordException(f"No record with {kwargs} found in {self.table_name}")
        except botocore.exceptions.ClientError as e:
            raise e
        return record



class Child(Parent):

    def __init__(self, *args, **kwargs):
        super().__init__(self, class_name=__name__, table_name="Child")
        self.record = {}

    def get_child_record(self, id):
        return self.get_record(Key={"id": id})

当我调用 Child().get_child_record(id) 时,我发现 self.table.get_item 被执行了两次。不仅如此,第二次它是用 record 本身而不是我调用函数的 args 执行的,结果低于错误。

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema 

我不明白是什么触发了这个。我认为这可能是因为 super() 所以我用父 class 名称替换了它,但没有白费。

如有任何帮助,我们将不胜感激。提前致谢。

我错误地调用了 get_record() ,这让我觉得事情在重复。通过使用子对象和父对象调用方法进行测试时,能够识别它。