golang-migrate/migrate · error

no database name

Error message

no database name

What it means

ErrNoDatabaseName in database/mongodb is returned by WithInstance, Open and WithConnection when the resolved database name (from the MongoDB URL path or Config.DatabaseName) is empty. MongoDB migrations need a target database to store the schema_migrations collection; without a name the driver cannot determine where to migrate.

Source

Thrown at database/mongodb/mongodb.go:39

func init() {
	db := Mongo{}
	database.Register("mongodb", &db)
	database.Register("mongodb+srv", &db)
}

var DefaultMigrationsCollection = "schema_migrations"

const DefaultLockingCollection = "migrate_advisory_lock" // the collection to use for advisory locking by default.
const lockKeyUniqueValue = 0                             // the unique value to lock on. If multiple clients try to insert the same key, it will fail (locked).
const DefaultLockTimeout = 15                            // the default maximum time to wait for a lock to be released.
const DefaultLockTimeoutInterval = 10                    // the default maximum intervals time for the locking timout.
const DefaultAdvisoryLockingFlag = true                  // the default value for the advisory locking feature flag. Default is true.
const LockIndexName = "lock_unique_key"                  // the name of the index which adds unique constraint to the locking_key field.
const contextWaitTimeout = 5 * time.Second               // how long to wait for the request to mongo to block/wait for.

var (
	ErrNoDatabaseName            = fmt.Errorf("no database name")
	ErrNilConfig                 = fmt.Errorf("no config")
	ErrLockTimeoutConfigConflict = fmt.Errorf("both x-advisory-lock-timeout-interval and x-advisory-lock-timout-interval were specified")
)

type Mongo struct {
	client   *mongo.Client
	db       *mongo.Database
	config   *Config
	isLocked atomic.Bool
}

type Locking struct {
	CollectionName string
	Timeout        int
	Enabled        bool
	Interval       int
}
type Config struct {

View on GitHub (pinned to 01a9643f14)

Solutions

  1. Append the database name to the MongoDB URL path: mongodb://host:27017/mydb
  2. Set Config.DatabaseName when using WithInstance/WithConnection
  3. Validate the DSN env var before constructing the driver

Example fix

// before
m, err := migrate.New("mongodb://localhost:27017", "file://migrations")
// after
m, err := migrate.New("mongodb://localhost:27017/mydb", "file://migrations")
Defensive patterns

Strategy: validation

Validate before calling

u, _ := url.Parse(dsn)
if strings.Trim(u.Path, "/") == "" {
    return fmt.Errorf("mongodb DSN must include a database name: mongodb://host/db")
}

Try / catch

if err != nil {
    if errors.Is(err, mongodb.ErrNoDatabaseName) {
        return fmt.Errorf("check MONGO_URL: missing database in path")
    }
    return err
}

Prevention

When it happens

Trigger: Opening "mongodb://host:27017/" with no database in the path and no DatabaseName in the config; WithInstance with a *mongo.Database built from an empty name; WithConnection where config.DatabaseName is "".

Common situations: Connection string missing the trailing database segment (mongodb://host vs mongodb://host/mydb), env-based DSNs with an unset DB name, renaming a database and leaving the env var empty.

Related errors


AI-assisted analysis of golang-migrate/migrate@01a9643f14 (2026-09-02). Data as JSON: /api/errors/71e68e1871df7710. Report an issue: GitHub.