# Buggy code
def prepare(self, stream, schema, extra):
    # Prepare package
    if "datapackage" not in extra or "resource-name" not in extra:
        return False
    descriptor = extra["datapackage"]
    if descriptor.strip().startswith("{"):
        descriptor = json.loads(descriptor)
    self.__package = Package(descriptor)

    # Prepare schema
    if not schema:
        return False
    if not schema.foreign_keys:
        return False
    self.__schema = schema

    # Prepare foreign keys values
    relations = _get_relations(
        self.__package, self.__schema, current_resource_name=extra["resource-name"]
    )
    self.__foreign_keys_values = _get_foreign_keys_values(self.__schema, relations)

    return True


# 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 prepare(self, stream, schema, extra):
    # Prepare package
    if "datapackage" not in extra or "resource-name" not in extra:
        return False
    descriptor = extra["datapackage"]
    if descriptor.strip().startswith("{"):
        descriptor = json.loads(descriptor)
    self.__package = datapackage.Package(descriptor)

    # Prepare schema
    if not schema:
        return False
    if not schema.foreign_keys:
        return False
    self.__schema = schema

    # Prepare foreign keys values
    try:
        self.__relations = _get_relations(
            self.__package, self.__schema, current_resource_name=extra["resource-name"]
        )
        self.__foreign_keys_values = _get_foreign_keys_values(
            self.__schema, self.__relations
        )
        self.__relations_exception = None
    except _ReferenceTableError as exception:
        self.__relations_exception = exception

    return True