Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions primary_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,18 @@ func (s *Sharding) createMySQLSequenceKeyIfNotExist(tableName string) error {
if stmt.Error != nil {
return fmt.Errorf("failed to create sequence table: %w", stmt.Error)
}
stmt = s.DB.Exec("INSERT INTO `" + mySQLSeqName(tableName) + "` VALUES (0)")
if stmt.Error != nil {
return fmt.Errorf("failed to insert into sequence table: %w", stmt.Error)
var count int
err := s.DB.Raw("SELECT COUNT(*) FROM `" + mySQLSeqName(tableName) + "`").Scan(&count).Error
if err != nil {
return fmt.Errorf("failed to check sequence table data: %w", err)
}

// Insert initial values only when there is no data in the table
if count == 0 {
stmt = s.DB.Exec("INSERT INTO `" + mySQLSeqName(tableName) + "` VALUES (0)")
if stmt.Error != nil {
return fmt.Errorf("failed to insert into sequence table: %w", stmt.Error)
}
}
return nil
}
Expand Down
Loading