Senior
Do you write SQL queries directly to the database or through an ORM?
sobes.tech AI
Answer from AI
Depending on the task, I use both direct SQL queries and ORM.
ORM is convenient for quick prototyping and working with simple CRUD operations; it abstracts database details and simplifies the code. However, for complex queries that require optimization or specific database features, I prefer to write SQL directly.
For example, in Go, ORM like GORM is often used, but raw SQL can be executed if needed:
// Example of executing raw SQL query in GORM
var users []User
result := db.Raw("SELECT * FROM users WHERE age > ?", 30).Scan(&users)
if result.Error != nil {
// handle error
}
This approach allows combining the convenience of ORM with the flexibility of direct queries for optimal performance and control.