I have a newbie doubt with slug in the quick guide

Im having a issue to follow the Articles exemple, in the quick guide. I did all steps reading the explications, but the result of the aplication cant add a Article, because an error with the slug’s storage.

I followed all the step
https://book.cakephp.org/3.0/en/quickstart.html#adding-simple-slug-generation
but the slug dont being storage, it is uping empty to the db

Can someone help me please?

cakephp

Hii there,

If you look in the tutorial’s database creation queries, you see the following:

CREATE TABLE articles (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    title VARCHAR(255) NOT NULL,
    slug VARCHAR(191) NOT NULL,
    body TEXT,
    published BOOLEAN DEFAULT FALSE,
    created DATETIME,
    modified DATETIME,
    UNIQUE KEY (slug),
    FOREIGN KEY user_key (user_id) REFERENCES users(id)
) CHARSET=utf8mb4;

Did you notice the UNIQUE KEY (slug) line?
It basically says that the slug column has to be a unique value.
This means that if you try to add the slug “i-am-a-slug” twice, it will throw this error.
This, however, is intended so that we won’t have two articles sharing a slug, so do not remove it.
In the documentation, it tells you the following:

This code is simple, and doesn’t take into account duplicate slugs. But we’ll fix that later on.

However, the tutorial never actually fixes this (I ran into this myself as well).

What I you should do is generate a slug, check whether it already exists in the database and repeat that until a unique slug is found (I generally just add a little number behind it like “i-am-a-slug”, “i-am-a-slug-2”, “i-am-a-slug-3” and so forth).
Dirty, but it works.

Happy baking!