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

Importing data with nested association #63

Closed
jorbascrumps opened this issue Sep 1, 2016 · 3 comments
Closed

Importing data with nested association #63

jorbascrumps opened this issue Sep 1, 2016 · 3 comments
Labels

Comments

@jorbascrumps
Copy link

I'm trying to write an import script for normalized address data with the following setup:

const Address = sequelize.define('address', { ... });
const Cities = sequelize.define('cities', { ... });
const Regions = sequelize.define('regions', { ... });

Address.belongsTo(Cities, {
    foreignKey: 'cityId'
});

Cities.belongsTo(Regions, {
    foreignKey: 'regionId'
});

And below is a sample of the data I'm trying to import:

Region import data:

ID Region
1 England

City import data:

ID City
1 London

Address import data:

ID Owner Address City Region
1 Sherlock Holmes 221B Baker Street London England

First, I import the region data, followed by the city data with a reference to the region:

// regions.json
[
    {
        "model": "Regions",
        "data": {
            "label": "England"
        }
    }
]

// cities.json
[
    {
        "model": "Cities",
        "data": {
            "label": "London",
            "region": {
                "label": "England"
            }
        }
    }
]

This works great, but is there a way to import the address data while referencing both the city and region data in order to prevent Holmes from living in London ON, CA? I've tried nesting region data inside of city:

// address.json
[
    {
        "model": "Address",
        "data": {
            "street_1": "221B Baker Street",
            "city": {
                "label": "London",
                "region": {
                    "label": "England"
                }
            }
        }
    }
]

I also tried to be clever and use Sequelize.literal which resulted in this:

[
    {
        "model": "Address",
        "data": {
            "street_1": "221B Baker Street",
            "cityId": {
                "val": "SELECT c.id FROM cities c LEFT JOIN regions r ON c.regionId = r.id WHERE c.label = \"London\" AND r.label = \"England\""
            }
        }
    }
]

Note: I've also posted this question on Stack Overflow.

@domasx2
Copy link
Owner

domasx2 commented Oct 10, 2016

Haven't encountered this before. I think the only decent solution right now is to specify IDs for the cities and use those

@matmar10
Copy link
Collaborator

Already answered, please reopen if you still need help @jorbascrumps

@jorbascrumps
Copy link
Author

Apologies, @domasx2, I hadn't seen your response until now! Thank you for reminding me about this issue, @matmar10.

I've closed the SO question and linked to the solution here.

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

No branches or pull requests

3 participants