<p>Here are some tips for trying to fix the duplicate key issue. There might be more answers relevant to this on <a href="https://help.openstreetmap.org" rel="nofollow">help.openstreetmap.org</a>, and it's worth asking follow-up questions there in case someone in the community has already figured it out.</p>
<ul>
<li>The first thing to check is whether the input file has duplicate nodes. If it is a <a href="https://wiki.openstreetmap.org/wiki/Planet.osm/full" rel="nofollow">"history"</a> file, then it is <em>not</em> suitable for loading with Osmosis' <code>--write-apidb</code> command. If it is a "normal" non-history file, it is possible that the file itself is broken. If you can get the same extract from somewhere else, then it might be worth trying that.</li>
<li>It has happened to me that I've tried to import into a database which I thought was empty, but actually contained data (in my case, from a previous failed run). It's worth checking that the database is truly empty of all fixtures, test data, previous runs, and so forth, before starting the import. If in doubt, drop and re-create the database. I don't use Rails itself to do this, as I don't trust it not to put in data that I don't know about; I just load the <code>db/structure.sql</code> file directly. This might be me cargo-culting, I'm not sure <g-emoji alias="wink" fallback-src="https://assets-cdn.github.com/images/icons/emoji/unicode/1f609.png" ios-version="6.0">😉</g-emoji></li>
<li>Adding data to an existing database <a href="https://help.openstreetmap.org/questions/51995/duplicate-key-value-violates-unique-constraint-relations_pkey" rel="nofollow">is not supported</a> by <code>--write-apidb</code>. If you do want to add data on top of existing data then you'll need to <a href="https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--derive-change_.28--dc.29" rel="nofollow">derive a change</a> between your two inputs and <a href="https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--write-apidb-change_.28--wdc.29" rel="nofollow">add only the change</a> to the database.</li>
<li>Finally, there is a complex method which can be used to clean the data, but I do not recommend it! It might cause more problems than it solves, but if you feel confident then give it a go. It's possible to remove duplicates by <a href="https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--derive-change_.28--dc.29" rel="nofollow">deriving the change</a> between <a href="https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--read-empty_.28--rem.29" rel="nofollow">an empty input</a> and your file, then <a href="https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--simplify-change_.28--simc.29" rel="nofollow">simplifying the change</a> and <a href="https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--apply-change_.28--ac.29" rel="nofollow">applying the change</a> to <a href="https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--read-empty_.28--rem.29" rel="nofollow">an empty input</a>.</li>
</ul>
<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/openstreetmap/openstreetmap-website/issues/1071#issuecomment-351975991">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/ABWnLQ2K8AE1tIYQLD2hAnIoM-Eb3zDSks5tAk9agaJpZM4GPndc">mute the thread</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/ABWnLWCSQhF0ZAMMR5FaWnxn2EfgthERks5tAk9agaJpZM4GPndc.gif" width="1" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
<link itemprop="url" href="https://github.com/openstreetmap/openstreetmap-website/issues/1071#issuecomment-351975991"></link>
<meta itemprop="name" content="View Issue"></meta>
</div>
<meta itemprop="description" content="View this Issue on GitHub"></meta>
</div>
<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/openstreetmap/openstreetmap-website","title":"openstreetmap/openstreetmap-website","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/openstreetmap/openstreetmap-website"}},"updates":{"snippets":[{"icon":"PERSON","message":"@zerebubuth in #1071: Here are some tips for trying to fix the duplicate key issue. There might be more answers relevant to this on [help.openstreetmap.org](https://help.openstreetmap.org), and it's worth asking follow-up questions there in case someone in the community has already figured it out.\r\n\r\n* The first thing to check is whether the input file has duplicate nodes. If it is a [\"history\"](https://wiki.openstreetmap.org/wiki/Planet.osm/full) file, then it is *not* suitable for loading with Osmosis' `--write-apidb` command. If it is a \"normal\" non-history file, it is possible that the file itself is broken. If you can get the same extract from somewhere else, then it might be worth trying that.\r\n* It has happened to me that I've tried to import into a database which I thought was empty, but actually contained data (in my case, from a previous failed run). It's worth checking that the database is truly empty of all fixtures, test data, previous runs, and so forth, before starting the import. If in doubt, drop and re-create the database. I don't use Rails itself to do this, as I don't trust it not to put in data that I don't know about; I just load the `db/structure.sql` file directly. This might be me cargo-culting, I'm not sure :wink:\r\n* Adding data to an existing database [is not supported](https://help.openstreetmap.org/questions/51995/duplicate-key-value-violates-unique-constraint-relations_pkey) by `--write-apidb`. If you do want to add data on top of existing data then you'll need to [derive a change](https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--derive-change_.28--dc.29) between your two inputs and [add only the change](https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--write-apidb-change_.28--wdc.29) to the database.\r\n* Finally, there is a complex method which can be used to clean the data, but I do not recommend it! It might cause more problems than it solves, but if you feel confident then give it a go. It's possible to remove duplicates by [deriving the change](https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--derive-change_.28--dc.29) between [an empty input](https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--read-empty_.28--rem.29) and your file, then [simplifying the change](https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--simplify-change_.28--simc.29) and [applying the change](https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--apply-change_.28--ac.29) to [an empty input](https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.46#--read-empty_.28--rem.29).\r\n"}],"action":{"name":"View Issue","url":"https://github.com/openstreetmap/openstreetmap-website/issues/1071#issuecomment-351975991"}}}</script>