The best way to find the issue is to simplify your code:
- Delete the "where" and "order" arguments.
- Delete the hasMany call (since your call originates from the Message model it's not relevant here).
- Delete the "joinKey" arguments (should only be needed when you have not set a primary key in the database table which I assume you have).
- Change foreignkey="mcatcode" to foreignkey="mcat" in the belongsTo (if I am reading your original SQL the foreign key is mcat and not mcatcode).
Try that and see if it works.
If it does. re-add the code that you removed... hopefully it still works :)
PS: I believe you can also remove the modelName argument since it will be deduced from the "name" argument.