preaction / Yancy

The Best Web Framework Deserves the Best Content Management System
http://preaction.me/yancy/
Other
54 stars 21 forks source link

read_schema failed to autoload the database fields except id #41

Closed smonff closed 5 years ago

smonff commented 5 years ago

Hi,

I use Yancy in a Mojolicious::Lite application through Mojolicious::Plugin::Yancy. The SQLIte database schema is :

CREATE TABLE votes (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    mood INT NOT NULL,
    date DATETIME NOT NULL, 
    active BOOLEAN DEFAULT (1) NOT NULL
);

And the app configuration is :

plugin Yancy => {
    backend => "sqlite:./data/rerb.sqlite",
    read_schema => 1,
    collections => {
        votes => {
            'x-list-columns' => [ 'id', 'mood', 'date', 'active' ],
        }
    }
};

Everything works fine this way, as long the x-list-columns parameters is available: if I remove it, the web page only display a list of id fields, but other fields are not display, despite they are returned by the Yancy API.

I report this, dunno if this is a bug or the expected behavior with maybe a lack of documentation? I assumed read_schema => 1 was able to "auto-list" all the collection fields, so that's why I think this behavior seems strange: I mean, by reading synopsis and various parts of the documentations (and trying different things many times), I was always having this problem and never found a place asking to explicitly set x-list-columns but always use read_schema.

If this is not a bug and that x-list-columns is required, I guess it should be specified more evidently. I volunteer to send a documentation PR.

Mojolicious 8.06 Yancy 1.018 Mojo::SQLite 3.001

Thanks!

preaction commented 5 years ago

Setting x-list-columns isn't required, but the heuristics that Yancy uses to guess what to display in the list (if you don't set x-list-columns) are crude at best: Right now, it looks for columns named 'id', 'name', 'username', 'title', or 'slug'.

My best idea for how to guess what columns to display is to display the first 4-6 columns that do not have "format": "textarea" or "format": "markdown". These columns are the most likely to break the list view (though, perhaps with some CSS, white-space: nowrap; text-overflow: ellipsis, we could keep things looking nice even with large amounts of text).

I've added a warning to the editor when the list only shows a single column that isn't a string (so, if it's showing an auto-increment id field), but if someone comes up with some better heuristics for guessing columns to show in the list, I'd be happy to accept it.

smonff commented 5 years ago

Thanks for the explanations and the hard work @preaction.

preaction commented 5 years ago

I'll open a new ticket for better x-list-columns heuristics.