Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Experimental external foreign keys support #239

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions data/datapackages_linked/cities/cities.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id,city
1,london
2,paris
3,rome
30 changes: 30 additions & 0 deletions data/datapackages_linked/cities/datapackage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"profile": "tabular-data-package",
"resources": [
{
"path": "cities.csv",
"profile": "tabular-data-resource",
"name": "cities",
"format": "csv",
"mediatype": "text/csv",
"encoding": "utf-8",
"schema": {
"fields": [
{
"name": "id",
"type": "integer",
"format": "default"
},
{
"name": "city",
"type": "string",
"format": "default"
}
],
"missingValues": [
""
]
}
}
]
}
40 changes: 40 additions & 0 deletions data/datapackages_linked/people/datapackage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"profile": "tabular-data-package",
"resources": [
{
"path": "people.csv",
"profile": "tabular-data-resource",
"name": "people",
"format": "csv",
"mediatype": "text/csv",
"encoding": "utf-8",
"schema": {
"fields": [
{
"name": "id",
"type": "integer",
"format": "default"
},
{
"name": "population",
"type": "integer",
"format": "default"
}
],
"missingValues": [
""
],
"foreignKeys": [
{
"fields": "id",
"reference": {
"package": "../cities/datapackage.json",
"resource": "cities",
"fields": "id"
}
}
]
}
}
]
}
4 changes: 4 additions & 0 deletions data/datapackages_linked/people/people.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id,population
1,8
2,2
4,3
44 changes: 32 additions & 12 deletions datapackage/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,24 +398,44 @@ def __get_table(self):

def __get_relations(self):
if not self.__relations:
table = self.__get_table()

# Prepare resources
resources = {}
if self.__get_table() and self.__get_table().schema:
for fk in self.__get_table().schema.foreign_keys:
resources.setdefault(fk['reference']['resource'], [])
for field in fk['reference']['fields']:
resources[fk['reference']['resource']].append(field)
if table and table.schema:
for fk in table.schema.foreign_keys:
resource_name = fk['reference'].get('resource')
package_name = fk['reference'].get('package')

# Self-referenced resource
if not resource_name:
resource = self

# Internal resource
elif not package_name:
if not self.__package:
continue
resource = self.__package.get_resource(resource_name)

# External resource (experimental)
# For now, we rely on uniqueness of resource names and relative paths
else:
from .package import Package
package = Package('/'.join([self.__base_path, package_name]))
resource = package.get_resource(resource_name)

# Check/add resource
if not resource:
message = 'Foreign key "%s" violation: resource not found'
message = message % fk['fields']
raise exceptions.RelationError(message)
resources[resource_name] = resource

# Fill relations
self.__relations = {}
for resource, fields in resources.items():
if resource and not self.__package:
continue
self.__relations.setdefault(resource, [])
data = self.__package.get_resource(resource) if resource else self
if data.tabular:
self.__relations[resource] = data.read(keyed=True)
for resource_name, resource in resources.items():
if resource.tabular:
self.__relations[resource_name] = resource.read(keyed=True)

return self.__relations

Expand Down