go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
37.02k stars 3.94k forks source link

Initialization with no default dialector #7145

Open KyleKotowick opened 3 months ago

KyleKotowick commented 3 months ago

Your Question

If we wish to use a DBResolver for multiple Sources and multiple Replicas, do we need to provide a default dialector in the first gorm.Open() (to create a DB handle that the DBResolver will be registered to)?

The source code suggests that the dialector argument in gorm.Open() is optional, as it only assigns a Dialector if the input is not nil.

However, other parts of the source code require a default dialector, such as running an Exec() (source). If you try to do an Exec() without providing a dialector in the gorm.Open(), it panics with a nil pointer error.

The document you expected this should be explained

https://gorm.io/docs/dbresolver.html

Expected answer

If there are multiple Sources, then the concept of a "default" shouldn't apply. Things that currently assume there's a default dialector should use the same logic as connection pools to select an appropriate dialector.