Create a new Engine instance using a configuration dictionary. The dictionary is typically produced from a config file. The keys of interest to ``engine_from_config()`` should be prefixed, e.g. ``sqlalchemy.url``, ``sqlalchemy.echo``, etc. The 'prefix' argument indicates the prefi
(
configuration: Dict[str, Any], prefix: str = "sqlalchemy.", **kwargs: Any
)
| 778 | |
| 779 | |
| 780 | def engine_from_config( |
| 781 | configuration: Dict[str, Any], prefix: str = "sqlalchemy.", **kwargs: Any |
| 782 | ) -> Engine: |
| 783 | """Create a new Engine instance using a configuration dictionary. |
| 784 | |
| 785 | The dictionary is typically produced from a config file. |
| 786 | |
| 787 | The keys of interest to ``engine_from_config()`` should be prefixed, e.g. |
| 788 | ``sqlalchemy.url``, ``sqlalchemy.echo``, etc. The 'prefix' argument |
| 789 | indicates the prefix to be searched for. Each matching key (after the |
| 790 | prefix is stripped) is treated as though it were the corresponding keyword |
| 791 | argument to a :func:`_sa.create_engine` call. |
| 792 | |
| 793 | The only required key is (assuming the default prefix) ``sqlalchemy.url``, |
| 794 | which provides the :ref:`database URL <database_urls>`. |
| 795 | |
| 796 | A select set of keyword arguments will be "coerced" to their |
| 797 | expected type based on string values. The set of arguments |
| 798 | is extensible per-dialect using the ``engine_config_types`` accessor. |
| 799 | |
| 800 | :param configuration: A dictionary (typically produced from a config file, |
| 801 | but this is not a requirement). Items whose keys start with the value |
| 802 | of 'prefix' will have that prefix stripped, and will then be passed to |
| 803 | :func:`_sa.create_engine`. |
| 804 | |
| 805 | :param prefix: Prefix to match and then strip from keys |
| 806 | in 'configuration'. |
| 807 | |
| 808 | :param kwargs: Each keyword argument to ``engine_from_config()`` itself |
| 809 | overrides the corresponding item taken from the 'configuration' |
| 810 | dictionary. Keyword arguments should *not* be prefixed. |
| 811 | |
| 812 | """ |
| 813 | |
| 814 | options = { |
| 815 | key[len(prefix) :]: configuration[key] |
| 816 | for key in configuration |
| 817 | if key.startswith(prefix) |
| 818 | } |
| 819 | options["_coerce_config"] = True |
| 820 | options.update(kwargs) |
| 821 | url = options.pop("url") |
| 822 | return create_engine(url, **options) |
| 823 | |
| 824 | |
| 825 | @overload |