Import CSV relationship error

Hi guys,

I am importing a CSV into neo4j and I am having a small issue with my Data model

I have the following data model after running this cypher query

Load csv with headers from 'file:///Congresional_Directory_Updated.csv' as line

With line
MERGE (c:Congressman{Address:line.Address})
ON CREATE SET
 c.Zip = line.`Zip+4`,
 c.BioguideID = line.BioguideID,
 c.Prefix = line.Prefix,
 c.Name = line.FullName

MERGE (city:City{Name:line.City})
MERGE (state:State{Code:line.State})
MERGE (district:District{District:line.`St/Dis`})
MERGE (party:Party{Name:line.Party})
MERGE (building:Building{Name:line.Building})

MERGE (c)-[:Lives_In]->(city)
MERGE (city)-[:Located_In]->(state)
MERGE (c)-[:Represents]->(party)
MERGE (c)-[:Works_In]->(building)

When I want to create a relationship between district and state after running the following Cypher query

Load csv with headers from 'file:///Congresional_Directory_Updated.csv' as line
With line 
MERGE (district:District{District:line.`St/Dis`})
MERGE (city:City{Name:line.City})
MERGE (district)-[:Located_In]->(city)

Unfortunately my data model turns to this

As you can see there are now multiple "Located_In" relationships that I didn't intend on having in my model. To be clear the desired "Located_In" relationships are between District and State as well as between City and State. I have circled them in the below image.

Any advice would be appreciated.

Many thanks,
Sam.

I'm a little confused. From your query you have:
MERGE (district)-[:Located_In]->(city)
which you said is something you don't want?

I don't see the MERGE for district located in the state.

Separately, please be aware that MERGE (city:City{Name:line.City}) is likely not what you want, as there are many cities with the same names that are in different states (Springfield, for example).

If you want to create your cities per state, then you need to do this instead:

MERGE (state:State{Code:line.State})
MERGE (city:City{Name:line.City})-[:Located_In]->(state)

Here's an article on how MERGE works that can help you out on understanding some of the subtler points of the clause.

Hi Andrew

thanks for your response and I really appreciate the heads up on cities with the same name, I apologies I made an error in my previous post.
I have circled the relationships I want below, I would appreciate your advice on how to avoid the ones I don't want.

Many thanks,
Sam.

Got it.

Let's see...you don't explicitly have an relationship MERGEs between a :District and a :State in your query, but you do have a curious (:City)-[:Located_In]->(:City) relationship in your graph that I'm guessing you aren't expecting to be there.

Is there by chance a :City node that is also labeled as a :State, such that it was caught up in a MATCH as something other than what it was meant to be?

MATCH (problem:City:State)
RETURN problem, problem.Name

That could explain both of the odd relationships being returned.

Otherwise, just look for the pattern that isn't supposed to be there and figure out if it's an outliner:

MATCH (d:District)-[:Located_In]->(s:State)
RETURN count(*)

And from there investigate further, and check the actual nodes and rels involved against your query and the CSV.

Hi,

I ran both query and they returned no records and a count of 0. Thanks for your time and help. There isnt a issue with the data in the database so Im happy enough that whatever error that's causing this funny Schema isnt that big of a deal.

Regards,
Sam.

Ah, interesting. Were you using db.schema(), or apoc.meta.graph()? I know that db.schema() sometimes has false positives in there.

I was using db.schema(). I get an error when I run apoc.meta.graph()

You'll need to have APOC Procedures installed to use apoc.meta.graph(). If you can, give that a try, it should be more accurate.

cheers

I'll give it a go

Can you also let us know what version of Neo4j you're using? I had thought we had fixed that bug earlier, but if it's still around we'll want to pass that along to engineering for fixing.

Im using desktp 1.2.1, and my graph is version 3.5.6