Skip to content

[OU-FIX] mail: deduplicate link previews by source URL - #15

Merged
cursor[bot] merged 1 commit into
20.0from
cursor/ou-20-08b-link-preview-dedupe-3e8f
Nov 12, 2026
Merged

cursor[bot] merged 1 commit into
20.0from
cursor/ou-20-08b-link-preview-dedupe-3e8f

Conversation

@adekock11

Copy link
Copy Markdown
Owner

Symptom

could not create unique index mail_link_preview_unique_source_url

Root cause

Odoo 20 mail.link.preview declares models.UniqueIndex("(source_url)") (_unique_source_url in addons/mail/models/mail_link_preview.py). The same URL was stored more than once. mail.message.link.preview points at those rows and itself has UNIQUE (message_id, link_preview_id). The upgrade analysis does not list this index (the generator does not emit models.UniqueIndex as a constraint line); the failure is the index create.

Change

Mail pre-migration, before the index is created:

  • Keep the lowest mail_link_preview.id per non-NULL source_url.
  • Delete mail_message_link_preview rows that would collide on (message_id, link_preview_id) once they point at the kept preview.
  • Re-point the remaining link_preview_id values.
  • Delete the duplicate previews.

The work file notes this next to mail.access_mail_link_preview_admin.

Fork 20.0 does not contain the mail migration yet (fork PR #4 does). This branch carries that pre-migration plus _dedupe_mail_link_preview. On a tree that already has PR #4, the only addition is that function, called at the start of migrate().

Verification

Index and the message-link unique index were read from the Odoo 20 models. ruff is clean. No database upgrade was run. No OCA pull request deduplicates mail.link.preview.

Upstream

Generic. Submit to OCA 20.0 with the mail migration. The index is new in 20.0.

Limitation

NULL source_url is not deduplicated (PostgreSQL unique indexes allow multiple NULLs; the field is required, so this is corrupt data). The kept row is the lowest id, not the most recently fetched preview. A foreign key other than mail_message_link_preview.link_preview_id would still block the delete; stock mail has no other one.

Open in Web Open in Cursor 

Keep the lowest mail.link.preview id per source_url, re-point message
links, and drop the extra link when one message had two previews of the
same URL. The migrate decorator stays on migrate.

Co-authored-by: André de Kock <[email protected]>
@cursor
cursor Bot force-pushed the cursor/ou-20-08b-link-preview-dedupe-3e8f branch from da981c4 to f4865c1 Compare November 12, 2026 08:50
@cursor
cursor Bot merged commit d6a8e2a into 20.0 Nov 12, 2026
6 of 8 checks passed
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

Successfully merging this pull request may close these issues.

2 participants