{"record":{"id":"4c93120f976561d2","repo":"pola-rs/polars","slug":"write-database-if-table-exists-must-be-one-of-a","errorCode":null,"errorMessage":"write_database `if_table_exists` must be one of {allowed}, got {if_table_exists!r}","messagePattern":"write_database `if_table_exists` must be one of (.+?), got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/dataframe/frame.py","lineNumber":4472,"sourceCode":"        ...     \"mssql+pyodbc://user:pass@server:1433/test?\"\n        ...     \"driver=ODBC+Driver+18+for+SQL+Server\"\n        ... )\n        >>> engine = create_engine(pyodbc_uri, fast_executemany=True)  # doctest: +SKIP\n        >>> df.write_database(\n        ...     table_name=\"target_table\",\n        ...     connection=engine,\n        ... )  # doctest: +SKIP\n\n        Returns\n        -------\n        int\n            The number of rows affected, if the driver provides this information.\n            Otherwise, returns -1.\n        \"\"\"\n        if if_table_exists not in (valid_write_modes := get_args(DbWriteMode)):\n            allowed = \", \".join(repr(m) for m in valid_write_modes)\n            msg = f\"write_database `if_table_exists` must be one of {{{allowed}}}, got {if_table_exists!r}\"\n            raise ValueError(msg)\n\n        connection_module_root = type(connection).__module__.split(\".\", 1)[0]\n\n        if engine is None:\n            if isinstance(connection, str) or connection_module_root == \"sqlalchemy\":\n                engine = \"sqlalchemy\"\n            elif connection_module_root.startswith(\"adbc\"):\n                engine = \"adbc\"\n\n        def unpack_table_name(name: str) -> tuple[str | None, str | None, str]:\n            \"\"\"Unpack optionally qualified table name to catalog/schema/table tuple.\"\"\"\n            from csv import reader as delimited_read\n\n            components: list[str | None] = next(delimited_read([name], delimiter=\".\"))  # type: ignore[arg-type]\n            if len(components) > 3:\n                msg = f\"`table_name` appears to be invalid: '{name}'\"\n                raise ValueError(msg)\n            catalog, schema, tbl = ([None] * (3 - len(components))) + components","sourceCodeStart":4454,"sourceCodeEnd":4490,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/dataframe/frame.py#L4454-L4490","documentation":"Error \"write_database `if_table_exists` must be one of {allowed}, got {if_table_exists!r}\" thrown in pola-rs/polars.","triggerScenarios":"Thrown at py-polars/src/polars/dataframe/frame.py:4472 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}