RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Fix: better GetByID (#20421)
Lynn8767071462ea
diff --git a/AGENTS.md b/AGENTS.mdindex 102adab8d7..a0016b30af 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -62,6 +62,10 @@ Use this file as the local operating guide for the current codebase. Prefer the- Remove commented-out Go code instead of leaving recovery notes in place.- Keep package comments and doc comments aligned with the current runtime path, not with migration history.+## GORM query traps+- **Never pass a bare string ID as the second argument to `Take`/`First`** (e.g. `Take(&x, id)`). Under GORM v1.25.7 a non-numeric string is treated as a raw SQL condition and concatenated into the `WHERE` clause: `Take(&x, "88b17c7a...")` emits `WHERE 88b17c7a... LIMIT 1`, which MySQL rejects with `Error 1054 (42S22): Unknown column '...' in 'where clause'`. Only strings that parse via `strconv.Atoi` are treated as primary-key values, so this bug is invisible for integer IDs and catastrophic for UUID primary keys. Always bind explicitly: `Take(&x, "id = ?", id)` or `Where("id = ?", id).Take(&x)`.+- When you change a query form across many files, verify the generated SQL with a `Session(&gorm.Session{DryRun: true})` probe before relying on it. A passing unit test with no rows and a broken `WHERE` clause look identical unless the SQL is inspected.+## Shared database schema (Go + Python)The Go services and the Python API write to the same MySQL/PostgreSQL schema, and both own parts of it. Neither is authoritative over the whole.diff --git a/internal/dao/api_for_conversation.go b/internal/dao/api_for_conversation.goindex 0b0cee85a9..989eae57ba 100644--- a/internal/dao/api_for_conversation.go+++ b/internal/dao/api_for_conversation.go@@ -146,13 +146,13 @@ func (dao *API4ConversationDAO) GetMetadataBySessionID(ctx context.Context, db *// agent. It is used when the session itself is the authorization resource.func (dao *API4ConversationDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.API4Conversation, error) {var result entity.API4Conversation- tx := db.WithContext(ctx).Session(&gorm.Session{QueryFields: true}).Where("id = ?", id).Find(&result)+ tx := db.WithContext(ctx).Session(&gorm.Session{QueryFields: true}).Take(&result, "id = ?", id)if tx.Error != nil {+ if errors.Is(tx.Error, gorm.ErrRecordNotFound) {+ return nil, nil+ }return nil, tx.Error}- if tx.RowsAffected == 0 {- return nil, nil- }if err := hydrateAPIConversations(ctx, db, []*entity.API4Conversation{&result}); err != nil {return nil, err}diff --git a/internal/dao/chat.go b/internal/dao/chat.goindex d7503ddbe5..452281bde0 100644--- a/internal/dao/chat.go+++ b/internal/dao/chat.go@@ -148,7 +148,7 @@ func (dao *ChatDAO) ListByOwnerIDs(ctx context.Context, db *gorm.DB, ownerIDs []// GetByID gets chat by IDfunc (dao *ChatDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.Chat, error) {var chat entity.Chat- err := db.WithContext(ctx).Where("id = ?", id).First(&chat).Error+ err := db.WithContext(ctx).Take(&chat, "id = ?", id).Errorif err != nil {return nil, err}diff --git a/internal/dao/chat_channel.go b/internal/dao/chat_channel.goindex 2ee9aadd47..4cc78e50ac 100644--- a/internal/dao/chat_channel.go+++ b/internal/dao/chat_channel.go@@ -21,7 +21,7 @@ func (dao *ChatChannelDAO) Create(ctx context.Context, db *gorm.DB, channel *ent// Callers must enforce tenant authorization themselves (see service.accessible).func (dao *ChatChannelDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.ChatChannel, error) {var channel entity.ChatChannel- if err := db.WithContext(ctx).Where("id = ?", id).First(&channel).Error; err != nil {+ if err := db.WithContext(ctx).Take(&channel, "id = ?", id).Error; err != nil {return nil, err}return &channel, nildiff --git a/internal/dao/chat_session.go b/internal/dao/chat_session.goindex 351161f5cc..ee9b0f4332 100644--- a/internal/dao/chat_session.go+++ b/internal/dao/chat_session.go@@ -57,7 +57,7 @@ func NewChatSessionDAO() *ChatSessionDAO {// GetByID gets chat session by IDfunc (dao *ChatSessionDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.ChatSession, error) {var conv entity.ChatSession- err := db.WithContext(ctx).Session(&gorm.Session{QueryFields: true}).Where("id = ?", id).First(&conv).Error+ err := db.WithContext(ctx).Session(&gorm.Session{QueryFields: true}).Take(&conv, "id = ?", id).Errorif err != nil {return nil, err}diff --git a/internal/dao/compilation_template.go b/internal/dao/compilation_template.goindex 170926e751..45fe9385db 100644--- a/internal/dao/compilation_template.go+++ b/internal/dao/compilation_template.go@@ -150,7 +150,7 @@ func (dao *CompilationTemplateDAO) NameExistsInGroup(ctx context.Context, db *go// (used for reconciling group children).func (dao *CompilationTemplateDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.CompilationTemplate, error) {var t entity.CompilationTemplate- if err := db.WithContext(ctx).Where("id = ?", id).First(&t).Error; err != nil {+ if err := db.WithContext(ctx).Take(&t, "id = ?", id).Error; err != nil {return nil, err}return &t, nildiff --git a/internal/dao/connector.go b/internal/dao/connector.goindex 1255eb73b1..8832580025 100644--- a/internal/dao/connector.go+++ b/internal/dao/connector.go@@ -203,7 +203,7 @@ func (dao *ConnectorDAO) LinkDatasetConnectorsTx(ctx context.Context, tx *gorm.D// GetByID get connector by IDfunc (dao *ConnectorDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.Connector, error) {var connector entity.Connector- err := db.WithContext(ctx).Where("id = ?", id).First(&connector).Error+ err := db.WithContext(ctx).Take(&connector, "id = ?", id).Errorif err != nil {return nil, err}diff --git a/internal/dao/document.go b/internal/dao/document.goindex 9a512e5bd3..6c86ca9285 100644--- a/internal/dao/document.go+++ b/internal/dao/document.go@@ -43,7 +43,7 @@ func (dao *DocumentDAO) Create(ctx context.Context, db *gorm.DB, document *entit// GetByID get document by IDfunc (dao *DocumentDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.Document, error) {var document entity.Document- err := db.WithContext(ctx).First(&document, "id = ?", id).Error+ err := db.WithContext(ctx).Take(&document, "id = ?", id).Errorif err != nil {return nil, err}diff --git a/internal/dao/file_commit.go b/internal/dao/file_commit.goindex 4ebf3276ae..007f74341d 100644--- a/internal/dao/file_commit.go+++ b/internal/dao/file_commit.go@@ -47,7 +47,7 @@ func NewFileCommitDAO() *FileCommitDAO {// GetByID gets a file commit by IDfunc (dao *FileCommitDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.FileCommit, error) {var commit entity.FileCommit- err := db.WithContext(ctx).Where("id = ?", id).First(&commit).Error+ err := db.WithContext(ctx).Take(&commit, "id = ?", id).Errorif err != nil {return nil, err}diff --git a/internal/dao/ingestion.go b/internal/dao/ingestion.goindex fc13ffa12a..55f3ed0d78 100644--- a/internal/dao/ingestion.go+++ b/internal/dao/ingestion.go@@ -212,7 +212,7 @@ func (dao *IngestionTaskDAO) ListByStatus(ctx context.Context, db *gorm.DB, statfunc (dao *IngestionTaskDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.IngestionTask, error) {var task *entity.IngestionTask- err := db.WithContext(ctx).Where("id = ?", id).First(&task).Error+ err := db.WithContext(ctx).Take(&task, "id = ?", id).Errorreturn task, err}diff --git a/internal/dao/mcp.go b/internal/dao/mcp.goindex c352c75c15..4651e701dc 100644--- a/internal/dao/mcp.go+++ b/internal/dao/mcp.go@@ -48,7 +48,7 @@ func NewMCPServerDAO() *MCPServerDAO {// GetByID returns an MCP server by ID.func (dao *MCPServerDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.MCPServer, error) {var server entity.MCPServer- if err := db.WithContext(ctx).Where("id = ?", id).First(&server).Error; err != nil {+ if err := db.WithContext(ctx).Take(&server, "id = ?", id).Error; err != nil {if errors.Is(err, gorm.ErrRecordNotFound) {return nil, nil}diff --git a/internal/dao/memory.go b/internal/dao/memory.goindex 29830cb402..4e35b7f2bc 100644--- a/internal/dao/memory.go+++ b/internal/dao/memory.go@@ -137,7 +137,7 @@ func (dao *MemoryDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*ent// GetByIDWithContext retrieves a memory record by ID from database with context.func (dao *MemoryDAO) GetByIDWithContext(ctx context.Context, db *gorm.DB, id string) (*entity.Memory, error) {var memory entity.Memory- err := db.WithContext(ctx).Where("id = ?", id).First(&memory).Error+ err := db.WithContext(ctx).Take(&memory, "id = ?", id).Errorif err != nil {return nil, err}diff --git a/internal/dao/pipeline_operation_log.go b/internal/dao/pipeline_operation_log.goindex c7275c8b4b..b163ad443d 100644--- a/internal/dao/pipeline_operation_log.go+++ b/internal/dao/pipeline_operation_log.go@@ -298,7 +298,7 @@ func (dao *PipelineOperationLogDAO) GetByIDAndKBID(ctx context.Context, db *gorm// GetByIDAndKBID instead.func (dao *PipelineOperationLogDAO) GetByID(ctx context.Context, db *gorm.DB, logID string) (*entity.PipelineOperationLog, error) {var log entity.PipelineOperationLog- if err := db.WithContext(ctx).Where("id = ?", logID).First(&log).Error; err != nil {+ if err := db.WithContext(ctx).Take(&log, "id = ?", logID).Error; err != nil {return nil, err}return &log, nildiff --git a/internal/dao/task.go b/internal/dao/task.goindex bc0251a06f..4a6e8df588 100644--- a/internal/dao/task.go+++ b/internal/dao/task.go@@ -49,7 +49,7 @@ func (dao *TaskDAO) CreateMany(ctx context.Context, db *gorm.DB, tasks []*entity// GetByID gets task by IDfunc (dao *TaskDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.Task, error) {var task entity.Task- err := db.WithContext(ctx).Where("id = ?", id).First(&task).Error+ err := db.WithContext(ctx).Take(&task, "id = ?", id).Errorif err != nil {return nil, err}diff --git a/internal/dao/tenant_model.go b/internal/dao/tenant_model.goindex 6e9e685010..48a5aacceb 100644--- a/internal/dao/tenant_model.go+++ b/internal/dao/tenant_model.go@@ -78,7 +78,7 @@ func (dao *TenantModelDAO) UpdateStatusByIDAndScope(ctx context.Context, db *gor// GetByID get tenant model by primary key (id)func (dao *TenantModelDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.TenantModel, error) {var model entity.TenantModel- err := db.WithContext(ctx).Where("id = ?", id).First(&model).Error+ err := db.WithContext(ctx).Take(&model, "id = ?", id).Errorif err != nil {return nil, err}diff --git a/internal/dao/tenant_model_group.go b/internal/dao/tenant_model_group.goindex 4a86b5bea6..e6b0b2f630 100644--- a/internal/dao/tenant_model_group.go+++ b/internal/dao/tenant_model_group.go@@ -34,7 +34,7 @@ func NewTenantModelGroupDAO() *TenantModelGroupDAO {// GetByID get tenant model group by primary key (id)func (dao *TenantModelGroupDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.TenantModelGroup, error) {var group entity.TenantModelGroup- err := db.WithContext(ctx).Where("id = ?", id).First(&group).Error+ err := db.WithContext(ctx).Take(&group, "id = ?", id).Errorif err != nil {return nil, err}diff --git a/internal/dao/tenant_model_instance.go b/internal/dao/tenant_model_instance.goindex 61803afdd0..01204f803d 100644--- a/internal/dao/tenant_model_instance.go+++ b/internal/dao/tenant_model_instance.go@@ -103,7 +103,7 @@ func (dao *TenantModelInstanceDAO) GetByProviderIDAndInstanceName(ctx context.Co// GetByID get tenant model instance by primary key (id)func (dao *TenantModelInstanceDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.TenantModelInstance, error) {var instance entity.TenantModelInstance- err := db.WithContext(ctx).Where("id = ?", id).First(&instance).Error+ err := db.WithContext(ctx).Take(&instance, "id = ?", id).Errorif err != nil {return nil, err}diff --git a/internal/dao/tenant_model_provider.go b/internal/dao/tenant_model_provider.goindex 857fbe5750..37b5f3ce95 100644--- a/internal/dao/tenant_model_provider.go+++ b/internal/dao/tenant_model_provider.go@@ -38,7 +38,7 @@ func (dao *TenantModelProviderDAO) Create(ctx context.Context, db *gorm.DB, prov// GetByID get tenant model provider by primary key (id)func (dao *TenantModelProviderDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.TenantModelProvider, error) {var provider entity.TenantModelProvider- err := db.WithContext(ctx).Where("id = ?", id).First(&provider).Error+ err := db.WithContext(ctx).Take(&provider, "id = ?", id).Errorif err != nil {return nil, err}diff --git a/internal/dao/user.go b/internal/dao/user.goindex c87bf95635..2f1caccae4 100644--- a/internal/dao/user.go+++ b/internal/dao/user.go@@ -39,7 +39,7 @@ func (dao *UserDAO) Create(ctx context.Context, db *gorm.DB, user *entity.User)// GetByID get user by IDfunc (dao *UserDAO) GetByID(ctx context.Context, db *gorm.DB, id uint) (*entity.User, error) {var user entity.User- err := db.WithContext(ctx).First(&user, id).Error+ err := db.WithContext(ctx).Take(&user, "id = ?", id).Errorif err != nil {return nil, err}diff --git a/internal/dao/user_canvas.go b/internal/dao/user_canvas.goindex 2398d32eaa..2bf3f22b55 100644--- a/internal/dao/user_canvas.go+++ b/internal/dao/user_canvas.go@@ -88,7 +88,7 @@ func (dao *UserCanvasDAO) Create(ctx context.Context, db *gorm.DB, userCanvas *e// GetByID get user canvas by IDfunc (dao *UserCanvasDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.UserCanvas, error) {var canvas entity.UserCanvas- err := db.WithContext(ctx).Where("id = ?", id).First(&canvas).Error+ err := db.WithContext(ctx).Take(&canvas, "id = ?", id).Errorif err != nil {if errors.Is(err, gorm.ErrRecordNotFound) {return nil, ErrUserCanvasNotFound