News for hackers. Stuff that matters.
Re:Database IDs Have No Place in URIs (2008) by cryptonector on Friday May 10, 2019 @01:02PM (#19879777)
Pick a primary key (i.e., set of columns) for a table. If you expose this outside the database in stable ways (e.g., in URIs), then do not permit changes to that table's rows' primary keys.

There's NO NEED for a primary key to be an INTEGER or any sort of ROWID.

I agree with TFA -if this is what it says- that one should not put INTEGER PRIMARY KEY values in URIs -- it's useless to users. Just make the "slug" a column, make a NOT NULL and UNIQUE, disallow changes, and use that. You don't have to make the slug a PRIMARY KEY -- NOT NULL and UNIQUE has the same semantics as PRIMARY KEY.

Surely TFA wouldn't argue to have no DB key at all in URIs, so I won't bother with that possibility. And yes, I should read TFA...

Re:Database IDs Have No Place in URIs (2008) by jabart on Friday May 10, 2019 @01:20PM (#19880002)
That is terrible advice.

1. Numeric based Primary Key Lookups are some of the fastest operations on modern databases. 2. "Slugs", are a set of strings, which occupy more memory, space, now you have to come up with some way to make them unique. If you are building a CMS, sure, SEO is important. If you are building a line of business application, no. 3. Just checked, Google and Facebook expose numeric based identifiers. My Facebook Business ID is numeric, my Google Analytics ID is numeric with a prefix on it. 4. There are plenty of libraries out there that will take a numeric value, convert it to a small alphanumeric value with a check digit. 5. No end user cares about URLs (except for SEO). Your business user is not going to navigate your application by going to myapp/settings, they are going to click on the gear icon.

Re:Database IDs Have No Place in URIs (2008) by derefr on Friday May 10, 2019 @01:17PM (#19879957)
What do you do when literally every "natural" column of your table is mutable, but you need the row's identity to be stable?

Well, you need what is called a https://en.wikipedia.org/wiki/Surrogate_key.

Surrogate keys need to be made up from whole cloth, apart from the natural data (because if they are derived from any of the natural data, they'll become incorrect when the data changes.) And what's easy to make up from whole cloth? Sequential integers. (Or random numbers, which—if you want to guarantee no collisions across a distributed DB cluster—gives you ≥128-bit random numbers, i.e. UUIDv4s.)

Re:Database IDs Have No Place in URIs (2008) by cryptonector on Friday May 10, 2019 @01:53PM (#19880390)
The slug has to be computed at INSERT time (e.g., by a trigger) and not allowed to change thereafter.
Re:Database IDs Have No Place in URIs (2008) by eridius on Friday May 10, 2019 @02:08PM (#19880563)
But that's no good because it means the slug may end up having no relation at all to the actual content of the page (after the page has been edited). The only thing worse than no slug is a bad slug. Don't bake bad slugs into your database design.
Re:Database IDs Have No Place in URIs (2008) by cryptonector on Friday May 10, 2019 @03:24PM (#19881331)
An integer has even less relation to the actual content.
Re:Database IDs Have No Place in URIs (2008) by eridius on Friday May 10, 2019 @04:32PM (#19881946)
That's why you pair the immutable integer with a mutable slug. Nobody memorizes slugs so you don't have to worry about supporting the case of a user manually typing in the URL from memory (which is about the only time when having a short integer in the URL would matter). With this scheme, you can change the slug at will, and in fact you can support loading the URL without having a slug at all, or with a made-up slug. This makes the URL resilient to minor transcription errors (e.g. accidentally deleting the last character of the URL, or screwing up when retyping it), and it also means anyone who's aware of this behavior can skip the slug entirely when retyping the URL.
Re:Database IDs Have No Place in URIs (2008) by xyzzyz on Friday May 10, 2019 @03:36PM (#19881477)
Yes, that's the whole point.
Re:Database IDs Have No Place in URIs (2008) by aflag on Friday May 10, 2019 @02:06PM (#19880531)
It needs to change if the title changes, though.
Re:Database IDs Have No Place in URIs (2008) by cryptonector on Friday May 10, 2019 @03:24PM (#19881332)
Why?
Re:Database IDs Have No Place in URIs (2008) by derefr on Friday May 10, 2019 @03:56PM (#19881647)
A lie is worse than nothing at all. Imagine if you will, a post originally written with the title of "Party A Wins Election", and thus the slug of "party-a-wins-election"; but not published in that state. Then, before publication, the facts that publication was waiting on come in, and the title is amended to "Party B Wins Election."

But now the slug in the URL of the published article (about party B winning the election) is still "party-a-wins-election"!

There are thousands of use-cases just like this. For another example, someone might have a real claim on a libel suit if you assert something false about them in a URL slug in a link hosted on your own site, even if it points to a page that says only true things about them! ("People don't always click through to find out what the actual article is about", their lawyer would argue; "sometimes they just read the URL itself." The judge would agree, being just the sort of person who is too busy to read everything that crosses their desk, instead triaging the importance of things [read oneself, delegate to their legal analysts, forget] by just such sloppy criteria as looking at the URL slug.)

This is why, in CMSes, you need to always be able to exert editorial control over every part of a page, including the URL. The slug, just like all other parts of the page, must be mutable. And so you can't use the slug as the primary key.

("But what if you just delete posts with bad slugs and make new posts to replace them?" Not always possible; in a CMS for a publisher with a hard-core editorial flow, often the "post" you see is just a view onto the same work-object that passes through the rest of the CMS process of edits and approvals, and you can't just delete the work-object and make another one, without getting whoever assigned you the job to cancel your assignment and re-assign it to you. Heck, often at time-of-assignment you don't even know what you're going to use as the headline. How should a system assign a slug for that?)

(And, while, yes, you could restructure things so as to decouple content-asset objects from business-domain objects, such that it's the content-asset object that has a slug, and maybe content-assets never change at all, just get new versions of them published with the previous versions hidden, with some router using rules to determine to which content-asset a given slug should route... at that point you're talking about the sort of Event Sourcing architecture which people on HN like to fantasize about but which 99% of businesses refuse to implement for the same reason they don't want to use unpopular programming languages: it makes it far harder to hire Joe Average Java-School Dev to maintain the code.)

Re:Database IDs Have No Place in URIs (2008) by cryptonector on Saturday May 11, 2019 @04:56PM (#19888351)
You keep a history of all the slugs, never reuse them, and have them all (but the current) redirect to the current. Done. No pesky integers in URIs.
Re:Database IDs Have No Place in URIs (2008) by aflag on Saturday May 11, 2019 @07:02PM (#19888914)
It seems a very cumbersome approach (and potentially slow) just to avoid ids in the URL. Whereas, having ids in the URL is simpler and has very few drawbacks.
Re:Database IDs Have No Place in URIs (2008) by wvenable on Friday May 10, 2019 @01:10PM (#19879871)
This seems like a lot of effort, with plenty of potentially fatal edge cases, for extremely little gain.

A small number in a URL isn't going to confuse anyone.

Re:Database IDs Have No Place in URIs (2008) by megous on Friday May 10, 2019 @01:48PM (#19880329)
> it's useless to users

It's pretty useful to me when scraping/syncing data from a website. I'm also a user.