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

Adding foreign keys #83

Closed
anuveyatsu opened this issue Apr 27, 2019 · 1 comment
Closed

Adding foreign keys #83

anuveyatsu opened this issue Apr 27, 2019 · 1 comment

Comments

@anuveyatsu
Copy link
Contributor

It could be just like set_primary_key processor, however, adding foreign keys is probably less common. At the moment, the only option I can see is to use update_resource processor by providing the entire schema of a resource. Is there a way to get generated schema so that I could just add a new key into it (e.g., foreignKeys)?

@anuveyatsu
Copy link
Contributor Author

After a bit of research, this can be easily done:

...

def add_foreign_keys(package):
    package.pkg.descriptor['resources'][0]['schema']['foreignKeys'] = [
        {
            'fields': 'Timestamp',
            'reference': {
                'resource': 'time',
                'fields': 'Timestamp'
            }
        }
    ]
    # Must yield the modified datapackage
    yield package.pkg
    # And its resources
    yield from package

Flow(
    load(...),
    add_foreign_keys,
    ...
).process()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant