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
)
| 800 | |
| 801 | |
| 802 | def engine_from_config( |
| 803 | configuration: Dict[str, Any], prefix: str = "sqlalchemy.", **kwargs: Any |
| 804 | ) -> Engine: |
| 805 | """Create a new Engine instance using a configuration dictionary. |
| 806 | |
| 807 | The dictionary is typically produced from a config file. |
| 808 | |
| 809 | The keys of interest to ``engine_from_config()`` should be prefixed, e.g. |
| 810 | ``sqlalchemy.url``, ``sqlalchemy.echo``, etc. The 'prefix' argument |
| 811 | indicates the prefix to be searched for. Each matching key (after the |
| 812 | prefix is stripped) is treated as though it were the corresponding keyword |
| 813 | argument to a :func:`_sa.create_engine` call. |
| 814 | |
| 815 | The only required key is (assuming the default prefix) ``sqlalchemy.url``, |
| 816 | which provides the :ref:`database URL <database_urls>`. |
| 817 | |
| 818 | A select set of keyword arguments will be "coerced" to their |
| 819 | expected type based on string values. The set of arguments |
| 820 | is extensible per-dialect using the ``engine_config_types`` accessor. |
| 821 | |
| 822 | :param configuration: A dictionary (typically produced from a config file, |
| 823 | but this is not a requirement). Items whose keys start with the value |
| 824 | of 'prefix' will have that prefix stripped, and will then be passed to |
| 825 | :func:`_sa.create_engine`. |
| 826 | |
| 827 | :param prefix: Prefix to match and then strip from keys |
| 828 | in 'configuration'. |
| 829 | |
| 830 | :param kwargs: Each keyword argument to ``engine_from_config()`` itself |
| 831 | overrides the corresponding item taken from the 'configuration' |
| 832 | dictionary. Keyword arguments should *not* be prefixed. |
| 833 | |
| 834 | """ |
| 835 | |
| 836 | options = { |
| 837 | key[len(prefix) :]: configuration[key] |
| 838 | for key in configuration |
| 839 | if key.startswith(prefix) |
| 840 | } |
| 841 | options["_coerce_config"] = True |
| 842 | options.update(kwargs) |
| 843 | url = options.pop("url") |
| 844 | return create_engine(url, **options) |
| 845 | |
| 846 | |
| 847 | @overload |