Answers.Body: <p>Take a database backup first to be on the safe side then run:</p>
  <pre><code>./craft utils/repair/section-structure yourStructureHandle
  </code></pre>
  <p>See documentation - <a href="https://craftcms.com/docs/3.x/console-commands.html#utils-repair-section-structure" rel="nofollow noreferrer">https://craftcms.com/docs/3.x/console-commands.html#utils-repair-section-structure</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-25T10:14:19.603Z
Answers.LastActivityDate: 2020-11-25T10:14:19.603Z
Answers.UserDisplayName: Oli
Body: <p>We migrated a craft cms site with 3 locales from v2.9 to 3.5
  We have a section with a field of type &quot;Entries&quot;.
  When editing the entry, and adding certain entries to this field, those get added twice after saving.
  After investigating, these relations dont show up twice in DB, but the SQL-query returns them twice.</p>
  <p>i created a test template to narrow down the SQL:</p>
  <p><code>{% set infos = craft.entries.id(497).one().contextInterneLinksRel.all() %}</code></p>
  <p>the sql resulting into double showing the relation:</p>
  <pre><code>SELECT `elements`.`id`, `elements`.`fieldLayoutId`, `elements`.`uid`, `elements`.`enabled`, `elements`.`archived`, `elements`.`dateCreated`, `elements`.`dateUpdated`, `elements_sites`.`id` AS `siteSettingsId`, `elements_sites`.`slug`, `elements_sites`.`siteId`, `elements_sites`.`uri`, `elements_sites`.`enabled` AS `enabledForSite`, `entries`.`sectionId`, `entries`.`typeId`, `entries`.`authorId`, `entries`.`postDate`, `entries`.`expiryDate`, `content`.`id` AS `contentId`, `content`.`title`, `content`.`field_absenderAuthorText`, `content`.`field_adminSpracheinstellungen`, `content`.`field_aktuellerStatus`, `content`.`field_medienmitteilungAnriss`, `content`.`field_showInList`, `content`.`field_begruessungstext`, `content`.`field_betreff`, `content`.`field_copyrightInformationenIntranet`, `content`.`field_medienmitteilungDatumVeroeffentlichung`, `content`.`field_deMailchimpNewsletterGdpr`, `content`.`field_deMailchimpNewsletterHoneypot`, `content`.`field_deMailchimpNewsletterUrl`, `content`.`field_globalEmailKursanmeldungenAlternativ`, `content`.`field_einleitungstext`, `content`.`field_globalAdresse`, `content`.`field_fileAlt`, `content`.`field_automatischesInhaltsverzeichnis`, `content`.`field_forumRBeschreibung`, `content`.`field_adminBildFocusCSSPosition`, `content`.`field_noTransform`, `content`.`field_blogAnrissText`, `content`.`field_fileCopyright`, `content`.`field_fileDescription`, `content`.`field_definitiveAnmeldung`, `content`.`field_einfacherInhalt`, `content`.`field_globalEmailKursanmeldungen`, `content`.`field_globalEmailVeranstaltungsanmeldungen`, `content`.`field_contextExterneLinks`, `content`.`field_portalLinkUrl`, `content`.`field_forumRAusgabe`, `content`.`field_kampagnenBoxInhalt`, `content`.`field_kampagnenBoxLink`, `content`.`field_kampagnenBoxLinkText`, `content`.`field_keineAnmeldungMoeglich`, `content`.`field_keinHeaderBild`, `content`.`field_kontaktFuerKurse`, `content`.`field_kontaktFuerVeranstaltungen`, `content`.`field_kostenMitglied`, `content`.`field_kostenNichtMitglied`, `content`.`field_kursKosten`, `content`.`field_notShowInNav`, `content`.`field_portalInhalt`, `content`.`field_portalLinkText`, `content`.`field_portalTitel`, `content`.`field_produktBeschreibung`, `content`.`field_produktPreis`, `content`.`field_produktReferenznummer`, `content`.`field_produktSteuer`, `content`.`field_globalRechteSpalteFusszeile`, `content`.`field_redirectUrl`, `content`.`field_regionkuerzel`, `content`.`field_seitenLayout`, `content`.`field_seitenueberschrift`, `content`.`field_metatagDescription`, `content`.`field_seitezugriffe`, `content`.`field_metatagTitle`, `content`.`field_frMailchimpNewsletterGdpr`, `content`.`field_frMailchimpNewsletterHoneypot`, `content`.`field_frMailchimpNewsletterUrl`, `content`.`field_fragenBemerkungen`, `content`.`field_itMailchimpNewsletterGdpr`, `content`.`field_itMailchimpNewsletterHoneypot`, `content`.`field_itMailchimpNewsletterUrl`, `content`.`field_newsletterausgabe`, `content`.`field_isShopStartseite`, `content`.`field_sprachenAbgeschlossen`, `content`.`field_subnavigationImmerZeigen`, `content`.`field_suchresultatText`, `content`.`field_test`, `structureelements`.`root`, `structureelements`.`lft`, `structureelements`.`rgt`, `structureelements`.`level`, `structureelements`.`structureId`
  FROM (SELECT `elements`.`id` AS `elementsId`, `elements_sites`.`id` AS `elementsSitesId`, `content`.`id` AS `contentId`, `structureelements`.`structureId`
  FROM `rhe_elements` `elements`
  INNER JOIN `rhe_entries` `entries` ON `entries`.`id` = `elements`.`id`
  INNER JOIN `rhe_elements_sites` `elements_sites` ON `elements_sites`.`elementId` = `elements`.`id`
  INNER JOIN `rhe_content` `content` ON (`content`.`elementId` = `elements`.`id`) AND (`content`.`siteId` = `elements_sites`.`siteId`)
  LEFT JOIN `rhe_structureelements` `structureelements` ON (`structureelements`.`elementId` = `elements`.`id`) AND (EXISTS (SELECT *
  FROM `rhe_structures`
  WHERE (`id` = `structureelements`.`structureId`) AND (`dateDeleted` IS NULL)))
  INNER JOIN `rhe_relations` `relations` ON (`relations`.`targetId` = `elements`.`id`) AND ((`relations`.`sourceId`=497) AND (`relations`.`fieldId`='85')) AND ((`relations`.`sourceSiteId` IS NULL) OR (`relations`.`sourceSiteId`=1))
  WHERE (`elements_sites`.`siteId`=1) AND (`elements`.`archived`=FALSE) AND (((`elements`.`enabled`=TRUE) AND (`elements_sites`.`enabled`=TRUE)) AND (`entries`.`postDate` &lt;= '2020-11-24 15:05:00') AND ((`entries`.`expiryDate` IS NULL) OR (`entries`.`expiryDate` &gt; '2020-11-24 15:05:00'))) AND (`elements`.`dateDeleted` IS NULL) AND (`elements`.`draftId` IS NULL) AND (`elements`.`revisionId` IS NULL)
  ORDER BY `relations`.`sortOrder`) `subquery`
  INNER JOIN `rhe_entries` `entries` ON `entries`.`id` = `subquery`.`elementsId`
  INNER JOIN `rhe_elements` `elements` ON `elements`.`id` = `subquery`.`elementsId`
  INNER JOIN `rhe_elements_sites` `elements_sites` ON `elements_sites`.`id` = `subquery`.`elementsSitesId`
  INNER JOIN `rhe_content` `content` ON `content`.`id` = `subquery`.`contentId`
  LEFT JOIN `rhe_structureelements` `structureelements` ON (`structureelements`.`elementId` = `subquery`.`elementsId`) AND (`structureelements`.`structureId` = `subquery`.`structureId`)
  INNER JOIN `rhe_relations` `relations` ON (`relations`.`targetId` = `elements`.`id`) AND ((`relations`.`sourceId`=497) AND (`relations`.`fieldId`='85')) AND ((`relations`.`sourceSiteId` IS NULL) OR (`relations`.`sourceSiteId`=1))
  ORDER BY `relations`.`sortOrder`
  </code></pre>
  <p>Here is an example for a double from the result:</p>
  <p><a href="https://i.sstatic.net/ikvfs.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ikvfs.png" alt="enter image description here" /></a></p>
  <p>The only difference are in columns: <code>root,lft,rgt,structureId</code> the rest is all the same.</p>
  <p><a href="https://i.sstatic.net/JYJSB.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/JYJSB.png" alt="enter image description here" /></a></p>
  <p>It seems there are 2 rows in table <code>structureelements</code> for this entry, that gets added twice.</p>
  <p><a href="https://i.sstatic.net/jxoBc.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/jxoBc.png" alt="enter image description here" /></a></p>
  <p>What does it mean? Any help is apreciated, thx.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-11-24T15:36:15.283Z
Comments.Text: Back up and try running `./craft utils/repair/section-structure yourStructureHandle`
Comments.UserDisplayName: Oli
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-11-25T08:00:33.640Z
Comments.Text: thx, works like a charme!
Comments.UserDisplayName: Chris Angel
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-11-25T10:12:34.030Z
Comments.Text: Great, I'll add it as an answer then!
Comments.UserDisplayName: Oli
Tags: <structure><migration><entries-field><sql>
Title: After Migration from v2.9 to 3.5 - some entries added into an entry-relation-field get added twice after saving
ThreadText: # After Migration from v2.9 to 3.5 - some entries added into an entry-relation-field get added twice after saving
  
  (Asked by: username_0 on 2020-11-24)
  
  We migrated a craft cms site with 3 locales from v2.9 to 3.5
  We have a section with a field of type "Entries".
  When editing the entry, and adding certain entries to this field, those get added twice after saving.
  After investigating, these relations dont show up twice in DB, but the SQL-query returns them twice.
  
  i created a test template to narrow down the SQL:
  
  `{% set infos = craft.entries.id(497).one().contextInterneLinksRel.all() %}`
  
  the sql resulting into double showing the relation:
  
  ```
  SELECT `elements`.`id`, `elements`.`fieldLayoutId`, `elements`.`uid`, `elements`.`enabled`, `elements`.`archived`, `elements`.`dateCreated`, `elements`.`dateUpdated`, `elements_sites`.`id` AS `siteSettingsId`, `elements_sites`.`slug`, `elements_sites`.`siteId`, `elements_sites`.`uri`, `elements_sites`.`enabled` AS `enabledForSite`, `entries`.`sectionId`, `entries`.`typeId`, `entries`.`authorId`, `entries`.`postDate`, `entries`.`expiryDate`, `content`.`id` AS `contentId`, `content`.`title`, `content`.`field_absenderAuthorText`, `content`.`field_adminSpracheinstellungen`, `content`.`field_aktuellerStatus`, `content`.`field_medienmitteilungAnriss`, `content`.`field_showInList`, `content`.`field_begruessungstext`, `content`.`field_betreff`, `content`.`field_copyrightInformationenIntranet`, `content`.`field_medienmitteilungDatumVeroeffentlichung`, `content`.`field_deMailchimpNewsletterGdpr`, `content`.`field_deMailchimpNewsletterHoneypot`, `content`.`field_deMailchimpNewsletterUrl`, `content`.`field_globalEmailKursanmeldungenAlternativ`, `content`.`field_einleitungstext`, `content`.`field_globalAdresse`, `content`.`field_fileAlt`, `content`.`field_automatischesInhaltsverzeichnis`, `content`.`field_forumRBeschreibung`, `content`.`field_adminBildFocusCSSPosition`, `content`.`field_noTransform`, `content`.`field_blogAnrissText`, `content`.`field_fileCopyright`, `content`.`field_fileDescription`, `content`.`field_definitiveAnmeldung`, `content`.`field_einfacherInhalt`, `content`.`field_globalEmailKursanmeldungen`, `content`.`field_globalEmailVeranstaltungsanmeldungen`, `content`.`field_contextExterneLinks`, `content`.`field_portalLinkUrl`, `content`.`field_forumRAusgabe`, `content`.`field_kampagnenBoxInhalt`, `content`.`field_kampagnenBoxLink`, `content`.`field_kampagnenBoxLinkText`, `content`.`field_keineAnmeldungMoeglich`, `content`.`field_keinHeaderBild`, `content`.`field_kontaktFuerKurse`, `content`.`field_kontaktFuerVeranstaltungen`, `content`.`field_kostenMitglied`, `content`.`field_kostenNichtMitglied`, `content`.`field_kursKosten`, `content`.`field_notShowInNav`, `content`.`field_portalInhalt`, `content`.`field_portalLinkText`, `content`.`field_portalTitel`, `content`.`field_produktBeschreibung`, `content`.`field_produktPreis`, `content`.`field_produktReferenznummer`, `content`.`field_produktSteuer`, `content`.`field_globalRechteSpalteFusszeile`, `content`.`field_redirectUrl`, `content`.`field_regionkuerzel`, `content`.`field_seitenLayout`, `content`.`field_seitenueberschrift`, `content`.`field_metatagDescription`, `content`.`field_seitezugriffe`, `content`.`field_metatagTitle`, `content`.`field_frMailchimpNewsletterGdpr`, `content`.`field_frMailchimpNewsletterHoneypot`, `content`.`field_frMailchimpNewsletterUrl`, `content`.`field_fragenBemerkungen`, `content`.`field_itMailchimpNewsletterGdpr`, `content`.`field_itMailchimpNewsletterHoneypot`, `content`.`field_itMailchimpNewsletterUrl`, `content`.`field_newsletterausgabe`, `content`.`field_isShopStartseite`, `content`.`field_sprachenAbgeschlossen`, `content`.`field_subnavigationImmerZeigen`, `content`.`field_suchresultatText`, `content`.`field_test`, `structureelements`.`root`, `structureelements`.`lft`, `structureelements`.`rgt`, `structureelements`.`level`, `structureelements`.`structureId`
  FROM (SELECT `elements`.`id` AS `elementsId`, `elements_sites`.`id` AS `elementsSitesId`, `content`.`id` AS `contentId`, `structureelements`.`structureId`
  FROM `rhe_elements` `elements`
  INNER JOIN `rhe_entries` `entries` ON `entries`.`id` = `elements`.`id`
  INNER JOIN `rhe_elements_sites` `elements_sites` ON `elements_sites`.`elementId` = `elements`.`id`
  INNER JOIN `rhe_content` `content` ON (`content`.`elementId` = `elements`.`id`) AND (`content`.`siteId` = `elements_sites`.`siteId`)
  LEFT JOIN `rhe_structureelements` `structureelements` ON (`structureelements`.`elementId` = `elements`.`id`) AND (EXISTS (SELECT *
  FROM `rhe_structures`
  WHERE (`id` = `structureelements`.`structureId`) AND (`dateDeleted` IS NULL)))
  INNER JOIN `rhe_relations` `relations` ON (`relations`.`targetId` = `elements`.`id`) AND ((`relations`.`sourceId`=497) AND (`relations`.`fieldId`='85')) AND ((`relations`.`sourceSiteId` IS NULL) OR (`relations`.`sourceSiteId`=1))
  WHERE (`elements_sites`.`siteId`=1) AND (`elements`.`archived`=FALSE) AND (((`elements`.`enabled`=TRUE) AND (`elements_sites`.`enabled`=TRUE)) AND (`entries`.`postDate` <= '2020-11-24 15:05:00') AND ((`entries`.`expiryDate` IS NULL) OR (`entries`.`expiryDate` > '2020-11-24 15:05:00'))) AND (`elements`.`dateDeleted` IS NULL) AND (`elements`.`draftId` IS NULL) AND (`elements`.`revisionId` IS NULL)
  ORDER BY `relations`.`sortOrder`) `subquery`
  INNER JOIN `rhe_entries` `entries` ON `entries`.`id` = `subquery`.`elementsId`
  INNER JOIN `rhe_elements` `elements` ON `elements`.`id` = `subquery`.`elementsId`
  INNER JOIN `rhe_elements_sites` `elements_sites` ON `elements_sites`.`id` = `subquery`.`elementsSitesId`
  INNER JOIN `rhe_content` `content` ON `content`.`id` = `subquery`.`contentId`
  LEFT JOIN `rhe_structureelements` `structureelements` ON (`structureelements`.`elementId` = `subquery`.`elementsId`) AND (`structureelements`.`structureId` = `subquery`.`structureId`)
  INNER JOIN `rhe_relations` `relations` ON (`relations`.`targetId` = `elements`.`id`) AND ((`relations`.`sourceId`=497) AND (`relations`.`fieldId`='85')) AND ((`relations`.`sourceSiteId` IS NULL) OR (`relations`.`sourceSiteId`=1))
  ORDER BY `relations`.`sortOrder`
  
  ```
  
  Here is an example for a double from the result:
  
  [![enter image description here](https://i.sstatic.net/ikvfs.png)](https://i.sstatic.net/ikvfs.png)
  
  The only difference are in columns: `root,lft,rgt,structureId` the rest is all the same.
  
  [![enter image description here](https://i.sstatic.net/JYJSB.png)](https://i.sstatic.net/JYJSB.png)
  
  It seems there are 2 rows in table `structureelements` for this entry, that gets added twice.
  
  [![enter image description here](https://i.sstatic.net/jxoBc.png)](https://i.sstatic.net/jxoBc.png)
  
  What does it mean? Any help is apreciated, thx.
  
  **Tags:** structure, migration, entries-field, sql
  
  > **Comments:**
  > **username_1**: Back up and try running `./craft utils/repair/section-structure yourStructureHandle`  
  > **username_0**: thx, works like a charme!  
  > **username_1**: Great, I'll add it as an answer then!  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-11-25. Score: 1
  
  Take a database backup first to be on the safe side then run:
  
  ```
  ./craft utils/repair/section-structure yourStructureHandle
  
  ```
  
  See documentation - <https://craftcms.com/docs/3.x/console-commands.html#utils-repair-section-structure>
  
  
  ---
  
  