golang-migrate/migrate · error
no database name
Error message
no database name
What it means
Sentinel error ErrNoDatabaseName defined in database/sqlserver/sqlserver.go. It is a generic guard indicating that no database name could be determined when opening the driver — typically because the connection URL had no path (e.g. sqlserver://host without a /dbname component). The same sentinel pattern exists in other drivers such as cockroachdb, where Open returns it when the parsed database name is empty. Fix by including the database name in the URL path.
Source
Thrown at database/sqlserver/sqlserver.go:29
"strings"
"sync/atomic"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
mssql "github.com/microsoft/go-mssqldb" // mssql support
)
func init() {
database.Register("sqlserver", &SQLServer{})
}
// DefaultMigrationsTable is the name of the migrations table in the database
var DefaultMigrationsTable = "schema_migrations"
var (
ErrNilConfig = fmt.Errorf("no config")
ErrNoDatabaseName = fmt.Errorf("no database name")
ErrNoSchema = fmt.Errorf("no schema")
ErrDatabaseDirty = fmt.Errorf("database is dirty")
ErrMultipleAuthOptionsPassed = fmt.Errorf("both password and useMsi=true were passed")
)
var lockErrorMap = map[int]string{
-1: "The lock request timed out.",
-2: "The lock request was canceled.",
-3: "The lock request was chosen as a deadlock victim.",
-999: "Parameter validation or other call error.",
}
// Config for database
type Config struct {
MigrationsTable string
DatabaseName string
SchemaName string
}View on GitHub (pinned to 01a9643f14)
Solutions
- Add database=<name> to the sqlserver:// URL query parameters
- Set config.DatabaseName when calling WithInstance/WithConnection
- Check the environment variable or secret supplying the database name is non-empty
Example fix
// before
d, err := migrate.Open("sqlserver://sa:pass@localhost:1433")
// after
d, err := migrate.Open("sqlserver://sa:pass@localhost:1433?database=mydb") Defensive patterns
Strategy: validation
Validate before calling
u, _ := url.Parse(dsn)
if u.Query().Get("database") == "" {
return errors.New("sqlserver DSN must include database=<name>")
} Type guard
func hasDatabase(u *url.URL) bool { return u.Query().Get("database") != "" } Prevention
- Fail fast at config load if DATABASE_NAME env is empty
- Always include ?database= in sqlserver migrate URLs
- Add DSN validation in a startup/config unit test
When it happens
Trigger: URL without ?database= (or InitialCatalog) and no config.DatabaseName; WithInstance with Config{DatabaseName: ""}.
Common situations: Empty/unset DATABASE_NAME env var interpolated into the connection string; switching between DSNs and URLs and losing the database segment; server connections with no default database.
Related errors
AI-assisted analysis of golang-migrate/migrate@01a9643f14 (2026-09-02).
Data as JSON: /api/errors/c74f51ffd7d38823.
Report an issue: GitHub.