groue / GRDB.swift

A toolkit for SQLite databases, with a focus on application development
MIT License
6.71k stars 694 forks source link

Creating an index isn't documented in the main readme anymore #1623

Open yspreen opened 5 days ago

yspreen commented 5 days ago

Links like the one in this post are now dead: https://forums.swift.org/t/how-to-add-an-index-for-a-column/48312

Would be nice to have an easily cmd+f'able headline right in the main readme. Other use-cases are explained there, like querying by index, I can't figure out how to create an index though.

groue commented 5 days ago

Hello @yspreen,

The documentation about the database schema is available at https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databaseschema

To create indexes, you can use one of the index creation methods, or profit from convenience apis:

try db.create(table: "player") { t in
    t.autoIncrementedPrimaryKey("id")

    // Creates an index or a unique index on the name column.
    t.column("name", .text).indexed()
    t.column("name", .text).unique()

    // Implicitly creates an index of the teamId foreign key.
    t.belongsTo("team")

    // Creates a unique index of the (a, b) columns.
    t.uniqueKey(["a", "b"])
}
yspreen commented 5 days ago

I appreciate the answer. I think it might be beneficial to have a section in the main readme file for creating indexes, since there are sections in that file for querying them. it seems slightly inconsistent

groue commented 5 days ago

Do you have anything specific in mind? There are already sections about the schema in the main README.

yspreen commented 4 days ago

I ended up using

try db.create(index: "index", on: "table", columns: ["column"], ifNotExists: true)

because I didn't know the .indexed convenience method was a thing. Might be useful to surface it with some searchable keywords (like "How to create an index") in the main readme. I would've greatly benefited from that!