From 3b18b9e1499fcef2184c200d2519426f89af018d Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 23 Nov 2025 21:11:27 -0800 Subject: [PATCH] Fix for REAL columns by CSV --detect-types Refs https://github.com/simonw/sqlite-utils/issues/645#issuecomment-3568947189 --- docs/cli-reference.rst | 2 +- sqlite_utils/cli.py | 2 +- sqlite_utils/db.py | 2 +- tests/test_cli.py | 24 +++++++++++++++++++++++- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/docs/cli-reference.rst b/docs/cli-reference.rst index 71b4ce0..007fb11 100644 --- a/docs/cli-reference.rst +++ b/docs/cli-reference.rst @@ -1142,7 +1142,7 @@ See :ref:`cli_add_column`. :: Usage: sqlite-utils add-column [OPTIONS] PATH TABLE COL_NAME - [[integer|int|float|text|str|blob|bytes]] + [[integer|int|float|real|text|str|blob|bytes]] Add a column to the specified table diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 7c7df37..f4fc43b 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -419,7 +419,7 @@ def dump(path, load_extension): @click.argument( "col_type", type=click.Choice( - ["integer", "int", "float", "text", "str", "blob", "bytes"], + ["integer", "int", "float", "real", "text", "str", "blob", "bytes"], case_sensitive=False, ), required=False, diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 3acf273..c057eab 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -216,7 +216,7 @@ COLUMN_TYPE_MAPPING = { "str": "TEXT", "integer": "INTEGER", "int": "INTEGER", - "float": "FLOAT", + "float": "REAL", "real": "REAL", "blob": "BLOB", "bytes": "BLOB", diff --git a/tests/test_cli.py b/tests/test_cli.py index bcf3e98..0b7a7de 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -284,7 +284,7 @@ def test_create_index_desc(db_path): "int", 'CREATE TABLE "dogs" (\n "name" TEXT\n, "integer" INTEGER)', ), - ("float", "FLOAT", 'CREATE TABLE "dogs" (\n "name" TEXT\n, "float" FLOAT)'), + ("float", "FLOAT", 'CREATE TABLE "dogs" (\n "name" TEXT\n, "float" REAL)'), ("blob", "blob", 'CREATE TABLE "dogs" (\n "name" TEXT\n, "blob" BLOB)'), ("blob", "BLOB", 'CREATE TABLE "dogs" (\n "name" TEXT\n, "blob" BLOB)'), ("blob", "bytes", 'CREATE TABLE "dogs" (\n "name" TEXT\n, "blob" BLOB)'), @@ -2240,6 +2240,28 @@ def test_upsert_detect_types(tmpdir, option): ] +def test_csv_detect_types_creates_real_columns(tmpdir): + """Test that CSV import with --detect-types creates REAL columns for floats""" + db_path = str(tmpdir / "test.db") + data = "name,age,weight\nCleo,6,45.5\nDori,1,3.5" + result = CliRunner().invoke( + cli.cli, + ["insert", db_path, "creatures", "-", "--csv", "--detect-types"], + catch_exceptions=False, + input=data, + ) + assert result.exit_code == 0 + db = Database(db_path) + # Check that the schema uses REAL for the weight column + assert db["creatures"].schema == ( + 'CREATE TABLE "creatures" (\n' + ' "name" TEXT,\n' + ' "age" INTEGER,\n' + ' "weight" REAL\n' + ")" + ) + + def test_integer_overflow_error(tmpdir): db_path = str(tmpdir / "test.db") result = CliRunner().invoke(