# Buggy code
def check_row(self, cells):
    row_number = cells[0]["row-number"]

    # Prepare keyed_row
    keyed_row = {}
    for cell in cells:
        if cell.get("field"):
            keyed_row[cell.get("field").name] = cell.get("value")

    # Resolve relations
    errors = []
    for foreign_key in self.__schema.foreign_keys:
        success = _resolve_relations(
            deepcopy(keyed_row), self.__foreign_keys_values, foreign_key
        )
        if success is None:
            message = 'Foreign key "{fields}" violation in row {row_number}'
            message_substitutions = {"fields": foreign_key["fields"]}

            # if not a composite foreign-key, add the cell causing the violation to improve the error details
            # with the column-number
            error_cell = None
            if len(foreign_key["fields"]) == 1:
                for cell in cells:
                    if cell["header"] == foreign_key["fields"][0]:
                        error_cell = cell
                        break

            errors.append(
                Error(
                    self.__code,
                    cell=error_cell,
                    row_number=row_number,
                    message=message,
                    message_substitutions=message_substitutions,
                )
            )

    return errors


# Traceback (datapackage.exceptions.CastError)
Traceback (most recent call last):
File "test.py", line 8, in <module>
report = validate("datapackage.json", checks=["structure", "schema", "foreign-key"], order_fields=True, infer_fields=False)
File "/home/didiez/anaconda3/lib/python3.7/site-packages/goodtables/validate.py", line 80, in validate
report = inspector.inspect(source, **options)
File "/home/didiez/anaconda3/lib/python3.7/site-packages/goodtables/inspector.py", line 82, in inspect
table_warnings, table_report = task.get()
File "/home/didiez/anaconda3/lib/python3.7/multiprocessing/pool.py", line 657, in get
raise self._value
File "/home/didiez/anaconda3/lib/python3.7/multiprocessing/pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "/home/didiez/anaconda3/lib/python3.7/site-packages/goodtables/inspector.py", line 200, in __inspect_table
success = prepare_func(stream, schema, extra)
File "/home/didiez/anaconda3/lib/python3.7/site-packages/goodtables/contrib/checks/foreign_key.py", line 48, in prepare
current_resource_name=extra['resource-name'])
File "/home/didiez/anaconda3/lib/python3.7/site-packages/goodtables/contrib/checks/foreign_key.py", line 116, in _get_relations
relations[resource_name] = resource.read(keyed=True)
File "/home/didiez/anaconda3/lib/python3.7/site-packages/datapackage/resource.py", line 377, in read
foreign_keys_values=foreign_keys_values, **options)
File "/home/didiez/anaconda3/lib/python3.7/site-packages/tableschema/table.py", line 353, in read
for count, row in enumerate(rows, start=1):
File "/home/didiez/anaconda3/lib/python3.7/site-packages/tableschema/table.py", line 215, in iter
for row_number, headers, row in iterator:
File "/home/didiez/anaconda3/lib/python3.7/site-packages/tableschema/table.py", line 509, in builtin_processor
row, row_number=row_number, exc_handler=exc_handler)
File "/home/didiez/anaconda3/lib/python3.7/site-packages/tableschema/schema.py", line 266, in cast_row
error_data=keyed_row)
File "/home/didiez/anaconda3/lib/python3.7/site-packages/tableschema/helpers.py", line 90, in default_exc_handler
raise exc
datapackage.exceptions.CastError: Row length 5 doesn't match fields count 6 for row "2"

# Fixed code
def check_row(self, cells):
    row_number = cells[0]["row-number"]
    errors = []

    # We DON'T have relations to validate
    if self.__relations_exception:
        # Add a reference error
        message = "Foreign key violation caused by invalid reference table: %s"
        errors.append(
            Error(
                self.__code,
                row_number=row_number,
                message=message % self.__relations_exception,
            )
        )

    # We have relations to validate
    else:
        # Prepare keyed_row
        keyed_row = {}
        for cell in cells:
            if cell.get("field"):
                keyed_row[cell.get("field").name] = cell.get("value")

        # Resolve relations
        for foreign_key in self.__schema.foreign_keys:
            success = _resolve_relations(
                deepcopy(keyed_row), self.__foreign_keys_values, foreign_key
            )
            if success is None:
                message = 'Foreign key "{fields}" violation in row {row_number}'
                message_substitutions = {"fields": foreign_key["fields"]}

                # if not a composite foreign-key, add the cell causing the violation to improve the error details
                # with the column-number
                error_cell = None
                if len(foreign_key["fields"]) == 1:
                    for cell in cells:
                        if cell["header"] == foreign_key["fields"][0]:
                            error_cell = cell
                            break

                # Add an error
                errors.append(
                    Error(
                        self.__code,
                        cell=error_cell,
                        row_number=row_number,
                        message=message,
                        message_substitutions=message_substitutions,
                    )
                )

    return errors