Skip to content

NULL value being mapped to false #109

@Harshit-Khasnis

Description

@Harshit-Khasnis

The following is the test that inserts NULL value in a boolean column type, it is being read as false,

func TestBooleanNullableColumn(t *testing.T) {
	DB.Migrator().DropTable(&BooleanTest{})
	DB.AutoMigrate(&BooleanTest{})
	// Insert a row with NULL value for Nullable column
	bt := BooleanTest{Flag: true, Nullable: nil}
	if err := DB.Create(&bt).Error; err != nil {
		t.Fatalf("failed to insert NULL bool: %v", err)
	}
	var got BooleanTest
	if err := DB.First(&got, bt.ID).Error; err != nil {
		t.Fatal(err)
	}
	if got.Nullable != nil {
		t.Errorf("expected NULL, got %v", *got.Nullable)
	}
}

These are the steps that are used to create the table through the sqlplus,

CREATE TABLE boolean_tests (
    id        NUMBER GENERATED BY DEFAULT AS IDENTITY,
    flag      NUMBER(1),        -- boolean-like
    nullable  NUMBER(1)
);

Inserted data

INSERT INTO boolean_tests (flag, nullable) VALUES (1, 1);
INSERT INTO BOOLEAN_TESTS (ID, FLAG) VALUES (1001, 1);

Output of the select statement

SQL> Select * from boolean_tests;

        ID       FLAG   NULLABLE
---------- ---------- ----------
         1          1          1
      1001          1

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions