What's "invalid" about this column name? `The colu...
# cfml-general
a
What's "invalid" about this column name?
The column name "EMPLOYEE_ID" is invalid. Column names must be valid variable names. They must start with a letter and can only include letters, numbers, and underscores.
b
What's the stack trace?
Lucee or Adobe?
j
What type of database as well.
a
Yes, I can dig all that up. But what I think you're saying is that "EMPLOYEE_ID" does not violate any of the query column naming rules to your knowledge? That's what I presumed, anyway.
It's CF2021, building a query from scratch from a CSV feed we download.
b
Is there whitespace in the excel spreadsheet that's being trimmed in the error message?
a
The first row is used to declare the columns. But since this function always works elsewhere, I'm gonna guess character set maybe?
Yep, let me check that, thanks
Nope trim() doesn't help. This is a queryAddColumn which is failing.
b
Can you paste the first row of the CSV here?
Also, stack trace?
a
"Employee Id","First Name","Last Name","Employee Status","Tree Name","Cost Full Name"
function *csvToQuery*(csvString){ var rowDelim = *chr*(10); var colDelim = ","; var numCols = 1; var newQuery = *QueryNew*(""); var arrayCol = *ArrayNew*(1); var i = 1; var j = 1; csvString = *trim*(csvString); if(*arrayLen*(arguments) GE 2) rowDelim = arguments[2]; if(*arrayLen*(arguments) GE 3) colDelim = arguments[3]; arrayCol = *listToArray*(*listFirst*(csvString,rowDelim),colDelim); colName = *trim*(*uCase*(*replace*(arrayCol[i],' ','_','all'))); for(i=1; i le *arrayLen*(arrayCol); i=i+1) *queryAddColumn*(newQuery,colName,*ArrayNew*(1)); for(i=2; i le *listLen*(csvString,rowDelim); i=i+1) { *queryAddRow*(newQuery); for(j=1; j le *arrayLen*(arrayCol); j=j+1) { if(*listLen*(*listGetAt*(csvString,i,rowDelim),colDelim) ge j) { *querySetCell*(newQuery,colName,*trim*(*replace*(*listGetAt*(*listGetAt*(csvString,i,rowDelim),j,colDelim),*chr*(34),'','all')), i-1); } } } return newQuery; }
So the function replaces any spaces in a column name with _
The only thing which has changed is the provider and their CSV feed. The function predates me and works.
Well, I added the trim() to the line setting colName
stack.txt
i
You are assigning only one value (
colName
) based on the first column name, and then using that same column name (
colName
) for every column, which is incorrect. That's why ColdFusion throws the error: > The column name "EMPLOYEE_ID" is invalid. Column names must be valid variable names. > They must start with a letter and can only include letters, numbers, and underscores. This error is misleading in your case - because the real problem is reusing the same column name (likely
"EMPLOYEE_ID"
) for all columns. Please try this:
function csvToQuery(csvString) {
var rowDelim = chr(10); // Default line break
var colDelim = ",";     // Default column separator
var newQuery = QueryNew("");
var columnNames = [];
var rows = [];
var i = 1;
var j = 1;
csvString = trim(csvString);
// Optional rowDelim and colDelim overrides
if (arrayLen(arguments) >= 2) rowDelim = arguments[2];
if (arrayLen(arguments) >= 3) colDelim = arguments[3];
// Split into rows
rows = listToArray(csvString, rowDelim);
// Parse header row and create columns
var headers = listToArray(rows[1], colDelim);
for (i = 1; i <= arrayLen(headers); i++) {
columnNames[i] = trim(uCase(replace(headers[i], " ", "_", "all")));
queryAddColumn(newQuery, columnNames[i], []);
}
// Parse and add remaining rows
for (i = 2; i <= arrayLen(rows); i++) {
var rowData = listToArray(rows[i], colDelim);
queryAddRow(newQuery);
for (j = 1; j <= arrayLen(columnNames); j++) {
if (j <= arrayLen(rowData)) {
var cellValue = trim(replace(rowData[j], chr(34), "", "all"));
querySetCell(newQuery, columnNames[j], cellValue, i - 1);
}
}
}
return newQuery;
}
a
Good catch, @Iftkhar - I had created that extra line while debugging. But after the application of your revised code the same error persists. 😔
From dumping some of the items like
rows
, I'm starting to suspect that this provider's csv file is not using chr 10 or 12 as cr/lf delimiters. Therefore, I think it's parsing the entire csv string's contents as the first or "headers" row.
OK, I have fixed the part which parses the first row into an array of proposed column headers
This line still throws the error on the first iteration thru the array
*queryAddColumn*(newQuery, columnNames[i], []);
_*The column name "EMPLOYEE_ID" is invalid.*_ Column names must be valid variable names. They must start with a letter and can only include letters, numbers, and underscores. So this little project is dead for the weekend. Sad for the project, happy for the weekend. 🎉
I also tried a different one first: _*The column name "FIRST_NAME" is invalid.*_
b
Wait a minute, are the quotes actually part of the name?
If so, that may be your issue
a
Oh for Benji's sake, look at that.
This
csvToQuery
function of theirs does a replace chr(34) on the cell contents, but not the headers. I need to dig up some old developer and slap him for my oversight.
Thanks Brad, you're much better than a rubber ducky.
👍 1