Skip to content

Model Class

columnNames()

columnNames() — returns string

Available in: model Category: Miscellaneous Functions

Returns a list of column names in the table mapped to this model. The list is ordered according to the columns’ ordinal positions in the database table.

// 1. Get the list of column names for the User model
cols = model("User").columnNames();
// cols -> "id,firstName,lastName,email,createdAt,updatedAt,deletedAt"

// 2. Check whether a specific column exists in the table
if (listFindNoCase(model("User").columnNames(), "email")) {
    writeOutput("The users table has an email column.");
}

// 3. Iterate over every column name
for (col in listToArray(model("User").columnNames())) {
    writeOutput(col);
}