flipped-aurora/gin-vue-admin · error

media.FileUploadAndDownload表数据初始化失败!

Error message

media.FileUploadAndDownload表数据初始化失败!

What it means

The media module's data initializer (InitializeData) seeds two demo rows into media.FileUploadAndDownload; if db.Create fails the error is wrapped as 'media.FileUploadAndDownload表数据初始化失败!'. This runs during system initialization when the init list registers the media seed data.

Source

Thrown at server/source/media/file_upload_download.go:50

	}
	return db.Migrator().HasTable(&media.FileUploadAndDownload{})
}

func (i *initFileMysql) InitializerName() string {
	return media.FileUploadAndDownload{}.TableName()
}

func (i *initFileMysql) InitializeData(ctx context.Context) (context.Context, error) {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return ctx, system.ErrMissingDBContext
	}
	entities := []media.FileUploadAndDownload{
		{Name: "10.png", Url: "https://qmplusimg.henrongyi.top/gvalogo.png", Tag: "png", Key: "158787308910.png"},
		{Name: "logo.png", Url: "https://qmplusimg.henrongyi.top/1576554439myAvatar.png", Tag: "png", Key: "1587973709logo.png"},
	}
	if err := db.Create(&entities).Error; err != nil {
		return ctx, errors.Wrap(err, media.FileUploadAndDownload{}.TableName()+"表数据初始化失败!")
	}
	return ctx, nil
}

func (i *initFileMysql) DataInserted(ctx context.Context) bool {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return false
	}
	lookup := media.FileUploadAndDownload{Name: "logo.png", Key: "1587973709logo.png"}
	if errors.Is(db.First(&lookup, &lookup).Error, gorm.ErrRecordNotFound) {
		return false
	}
	return true
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Read the wrapped cause: 'no such table' → ensure AutoMigrate includes media.FileUploadAndDownload and ran on the same DB instance selected in the init form.
  2. Check for pre-existing conflicting rows (duplicate key) and clear them: DELETE FROM media_file_upload_and_downloads; then re-run init.
  3. Verify config.yaml system.db-type/datasource credentials and that the DB user has INSERT permission on the database.

Example fix

// before: seeding before table exists
InitializeData(ctx) // no such table
// after: migrate first
db.AutoMigrate(&media.FileUploadAndDownload{})
InitializeData(ctx)
Defensive patterns

Strategy: try-catch

Validate before calling

if err := db.AutoMigrate(&media.FileUploadAndDownload{}); err != nil {
  return ctx, fmt.Errorf("migrate media table before seeding: %w", err)
}

Try / catch

ctx, err := initFileMysql.InitializeData(ctx)
if err != nil {
  if strings.Contains(err.Error(), "表数据初始化失败") {
    // check table exists & clear conflicting rows, then retry init
    log.Printf("media seed failed, verify table & privileges: %v", err)
  }
  return ctx, err
}

Prevention

When it happens

Trigger: System init flow when: the media_file_upload_and_downloads table does not exist (AutoMigrate skipped or ran on another DB), the DB connection is down/misconfigured, a unique key conflict exists, or the DB user lacks INSERT privileges.

Common situations: Fresh deployment where migration ran against a different datasource than the seeding ctx db; manual table creation with a mismatched schema; partial init rerun after a failed earlier attempt left conflicting rows; MySQL connection refused during setup wizard.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/14882a2e170cee9e. Report an issue: GitHub.