Django: Ticket #5819: FlatPage model field url is not uniquehttps://code.djangoproject.com/ticket/5819
<p>
You can create several pages with the same URL, which makes the middleware barf.
</p>
<p>
I'm not submitting a patch, but I'd imagine it to be as easy as adding an index on (url, site_id).
</p>
en-usDjangohttps://www.djangoproject.com/s/img/site/hdr_logo.gifhttps://code.djangoproject.com/ticket/5819
Trac 1.2.2arien <regexbot@…>Sat, 27 Oct 2007 05:52:55 GMTattachment sethttps://code.djangoproject.com/ticket/5819
https://code.djangoproject.com/ticket/5819
<ul>
<li><strong>attachment</strong>
set to <em>5819.diff</em>
</li>
</ul>
<p>
trivial patch to ensure url is unique
</p>
Ticketarien <regexbot@…>Sat, 27 Oct 2007 06:01:21 GMThas_patch changedhttps://code.djangoproject.com/ticket/5819#comment:1
https://code.djangoproject.com/ticket/5819#comment:1
<ul>
<li><strong>has_patch</strong>
set
</li>
</ul>
TicketJesper Noehr <jesper@…>Sat, 27 Oct 2007 09:25:26 GMTneeds_better_patch changedhttps://code.djangoproject.com/ticket/5819#comment:2
https://code.djangoproject.com/ticket/5819#comment:2
<ul>
<li><strong>needs_better_patch</strong>
set
</li>
</ul>
<p>
Arien,
</p>
<p>
I don't think it's an appropriate fix to set unique=True on url alone. Flatpages are per site, and sites can have the same urls. I think the unique constraint needs to be on (url, site_id) as I mentioned in my initial report.
</p>
Ticketarien <regexbot@…>Sat, 27 Oct 2007 11:10:12 GMThttps://code.djangoproject.com/ticket/5819#comment:3
https://code.djangoproject.com/ticket/5819#comment:3
<p>
Right you are; I can't believe I missed that both in your description and the docs...!
</p>
<p>
I don't think this can be fixed in a meaningful way without changing the many-to-many relationship between FlatPages and Sites. For example, what should happen if you create an /about/ page and decide to use it on more than one site, and then later you add create another /about/ page for one of these same sites (and possibly others)?
</p>
TicketJesper Noehr <jesper@…>Sat, 27 Oct 2007 12:52:30 GMThttps://code.djangoproject.com/ticket/5819#comment:4
https://code.djangoproject.com/ticket/5819#comment:4
<p>
Hm, you have a good point. Although--without knowing the inner workings on ManyToMany--I do believe that if the unique constraint lives on (url, site_id), then you *can* use it cross-site. E.g.
</p>
<p>
('/about/', 1) and ('/about/', 2) would be fine with that constraint. However, you cannot create a new ('/about', 2) anymore, since the contraint won't allow you to. Please correct me if I'm wrong :-)
</p>
Ticketarien <regexbot@…>Mon, 29 Oct 2007 09:40:21 GMTneeds_better_patch, has_patch changedhttps://code.djangoproject.com/ticket/5819#comment:5
https://code.djangoproject.com/ticket/5819#comment:5
<ul>
<li><strong>needs_better_patch</strong>
unset
</li>
<li><strong>has_patch</strong>
unset
</li>
</ul>
<p>
You cannot have a unique constraint across tables.
</p>
<p>
You could prevent creating duplicates by checking what combinations are "taken" before saving: you raise an exception if the FlatPage you're about to save has one or more Sites that are already used by another FlatPage (i.e. one with a different id) with the same URL (path, really). But you'd have to do that in a transaction, and since not all databases used with Django are guaranteed to have those...
</p>
<p>
Changing <a class="ext-link" href="http://code.djangoproject.com/browser/django/trunk/django/contrib/flatpages/views.py"><span class="icon">​</span>the view</a> to use get_list_or_404 (and picking one of the results) instead of get_object_or_404 isn't really an option either, so I think punting was probably the right decision.
</p>
TicketJesper Noehr <jesper@…>Mon, 29 Oct 2007 10:58:36 GMThttps://code.djangoproject.com/ticket/5819#comment:6
https://code.djangoproject.com/ticket/5819#comment:6
<p>
I don't entirely agree with that. The constraint is not across tables, as I'm not suggesting (url, site) but (url, site_ID). Both columns are in the same table, so it's a simple unique constraint.
</p>
TicketJames BennettMon, 29 Oct 2007 16:43:27 GMThttps://code.djangoproject.com/ticket/5819#comment:7
https://code.djangoproject.com/ticket/5819#comment:7
<p>
Replying to <a class="ticket" href="https://code.djangoproject.com/ticket/5819#comment:6" title="Comment 6">Jesper Noehr &lt;jesper@noehr.org&gt;</a>:
</p>
<blockquote class="citation">
<p>
I don't entirely agree with that. The constraint is not across tables, as I'm not suggesting (url, site) but (url, site_ID). Both columns are in the same table, so it's a simple unique constraint.
</p>
</blockquote>
<p>
No, they are <strong>not</strong> in the same table; the <code>url</code> column is in the flatpages table, and the <code>site_id</code> column is in a separate join table used to implement the many-to-many relationship. Go look at your database if you don't believe me.
</p>
TicketanonymousMon, 29 Oct 2007 16:49:52 GMThttps://code.djangoproject.com/ticket/5819#comment:8
https://code.djangoproject.com/ticket/5819#comment:8
<p>
I believe you, thank you for clarifying. As I wrote earlier, I don't know how manytomany relationships work in Django, but of course they would be linked through a link-table, now that you mention it.
</p>
<p>
I'm not really sure what to do about the constraint then.
</p>
Ticketarien <regexbot@…>Fri, 09 Nov 2007 01:34:01 GMTstatus changed; resolution sethttps://code.djangoproject.com/ticket/5819#comment:9
https://code.djangoproject.com/ticket/5819#comment:9
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>duplicate</em>
</li>
</ul>
<p>
This is a duplicate of <a class="closed ticket" href="https://code.djangoproject.com/ticket/702" title="#702: New feature: document that ManyToMany fields can't be in unique_together (closed: fixed)">#702</a>.
</p>
Ticket