golang · go · gorm · mysql · sql · syntax error · keyword

GORM and field name vs keyword conflict | Time is Expensive resource

This post about small compromises through doing workarounds.
Here in post I’ll pointing to fact that Time Is Golden need to get things done instead of stubbornly fighting the bug.


Today during development of puppet project - old fashioned mobile targeted social network for Azerbaijanian friends I’ve encountered interested bug.

It says (I’ll put some spacings to point to idea):

2025/07/28 13:59:34 /apps/aznet-backend/internal/repositories/album_repository.go:25 Error 1064 (42000): You have an error in your SQL syntax; 
check the manual that corresponds to your MariaDB server version for the right syntax to use 
near 'DESC, avatars DESC, created_at ASC' at line 1   

# debugging tip "near DESC" - means to left of that point

[0.152ms] [rows:0] SELECT * FROM `user_albums` WHERE user_id = 'NZtYVORpNe-Zd97AlJtsU6aB6Fa9Hls7' AND deleted = false ORDER BY default DESC, avatars DESC, created_at ASC

2025/07/28 13:59:34 Error getting albums for user NZtYVORpNe-Zd97AlJtsU6aB6Fa9Hls7: Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DESC, avatars DESC, created_at ASC' at line 1

let’s analyze query:

SELECT 
   * 
FROM 
  `user_albums` 
WHERE 
  user_id = 'NZtYVORpNe-Zd97AlJtsU6aB6Fa9Hls7' 
  AND 
  deleted = false 
ORDER BY 
  default DESC, 
  avatars DESC, 
  created_at ASC

As we can see we have field called “default” which is keyword in SQL dialect(s).

So I decided to simply add backticks to make default to be:

`default`

Easy! I thought:

type UserAlbum struct {
	ID        string    `gorm:"type:char(32);primaryKey" json:"id"`
	UserID    string    `gorm:"type:char(32);not null;index" json:"userId"`
	Name      string    `gorm:"type:char(200);not null" json:"name"`
	Default bool        `gorm:"column:default;type:boolean;default:false" json:"isDefault"`
	Avatars bool        `gorm:"column:avatars;type:boolean;default:false" json:"isAvatars"`
	Deleted   bool      `gorm:"type:boolean;default:false" json:"deleted"`
	CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"`
	UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updatedAt"`

	// Relations
	User       User        `gorm:"foreignKey:UserID" json:"user,omitempty"`
	UserPhotos []UserPhoto `gorm:"foreignKey:AlbumID" json:"userPhotos,omitempty"`
}

simply add backticks!

Default bool  `gorm:"column:`default`;type:boolean;default:false" json:"isDefault"`
Avatars bool  `gorm:"column:avatars;type:boolean;default:false" json:"isAvatars"`

But! Nope! It breaks annotation now.

So as solution I decided to replace default field with is_default.

Why?

I had 2 ways:

  1. to write issue, create PR and wait when it will be available and miss time waiting

  2. to solve issue simply adding is_ prefix which does not conflict with SQL dialect(s) keywords


P.S. It’s just a small challenge in life of developer which he/she encounter.
Me personally happen to see such many times and solved by described way.
Idea of this post simply to point that most of times it’s better to just do workarounds instead of insisting or fighting the library/structure which not under your control.


fin