Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data corrupted when mapped from UUID array #412

Open
illusionare opened this issue Oct 21, 2024 · 4 comments
Open

Data corrupted when mapped from UUID array #412

illusionare opened this issue Oct 21, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@illusionare
Copy link

Describe the bug
In database I have 2 tables with relation one-to-many (both has ID columns of type uuid). I am making a select with a join of these two tables. The result object contains id field of parent table and an array of ids of child table. Data in the array of child ids are corrupted. The reason: the array of UUIDS is treated as an array of simple type and elements(e.g. uuid) created from first 16 bytes of the string.

Environment (please complete the following information):

  • OS: linux
  • Database: postgres
  • Database driver: pq
  • Jet version: v2.11.1

Code snippet

Code for database:

CREATE TABLE main (
                      id UUID PRIMARY KEY
);

CREATE TABLE second (
                        main_id UUID,
                        second_id UUID,
                        FOREIGN KEY (main_id) REFERENCES main (id)
);

Generated models:

type Main struct {
    ID uuid.UUID \`sql:"primary_key"\`
}

type Second struct {
    MainID   *uuid.UUID
    SecondID *uuid.UUID
}

Code to run:

func main() {
	db, err := sql.Open("postgres", dbConnStr)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// ARRANGE
	mainID := uuid.UUID{} // with empty UUID it is easier to see error
	secondID := uuid.UUID{}

	//InsertData(db, mainID, secondID)

	// ACT
	queryStmt := postgres.
		SELECT(table.Main.ID, table.Second.SecondID).
		FROM(table.Main.INNER_JOIN(table.Second, table.Second.MainID.EQ(table.Main.ID))).
		WHERE(table.Second.MainID.EQ(postgres.UUID(mainID)))

	var composite []Composite
	err = queryStmt.Query(db, &composite)
	if err != nil {
		log.Fatalf("Error querying second: %v", err)
	}
	// ASSERT
	fmt.Printf("Original: mainID:%v secondID:%v\n", mainID, secondID)
	fmt.Printf("Original: mainID:%v secondID:%v\n", composite[0].Main, composite[0].SecondId[0])
	fmt.Printf("Original: mainID:%v secondID:%v <--\n", composite[0].Main, composite[0].SecondId2[0])
	fmt.Printf("Original: mainID:%v secondID:%v\n", composite[0].Main, composite[0].SecondId3)
	fmt.Printf("Original: mainID:%v secondID:%v\n", composite[0].Main, composite[0].SecondId4[0].SecondId)
}

func InsertData(db *sql.DB, mainID uuid.UUID, secondID uuid.UUID) {
	insertMainStmt := table.Main.
		INSERT(table.Main.ID).
		VALUES(mainID)

	_, err := insertMainStmt.Exec(db)
	if err != nil {
		log.Fatalf("Error inserting into main: %v", err)
	}
	fmt.Printf("Inserted main entity with ID: %s\n", mainID)

	insertSecondStmt := table.Second.
		INSERT(table.Second.MainID, table.Second.SecondID).
		VALUES(mainID, secondID)

	_, err = insertSecondStmt.Exec(db)
	if err != nil {
		log.Fatalf("Error inserting into second: %v", err)
	}
}

Composite class:

type Proxy struct {
	SecondId uuid.UUID `alias:"Second.second_id"`
}

type Composite struct {
	model.Main
	SecondId  []string    `alias:"Second.second_id"`
	SecondId2 []uuid.UUID `alias:"Second.second_id"` // this is an version of errors
	SecondId3 uuid.UUID   `alias:"Second.second_id"`
	SecondId4 []Proxy     `alias:"Second.*"`
}

Expected behavior
I am expecting that array of UUID`s will have correct values.

@illusionare illusionare added the bug Something isn't working label Oct 21, 2024
@houten11
Copy link

What do you mean by data corruption? queryStmt.Query(db, &composite) always returns this error: Error querying second: jet: can't append string to []uuid.UUID slice: can't assign string to uuid.UUID.

@go-jet
Copy link
Owner

go-jet commented Oct 23, 2024

Couldn't reproduce data corruption. Query always returns an error. But I agree scan into slice of Scanner type should work as well.

What UUID type are you using?

@illusionare
Copy link
Author

I am using "github.com/google/uuid"

@illusionare
Copy link
Author

What do you mean by data corruption? queryStmt.Query(db, &composite) always returns this error: Error querying second: jet: can't append string to []uuid.UUID slice: can't assign string to uuid.UUID.

Well, i am not receiving any errors in this example. My output is:

Original: mainID:00000000-0000-0000-0000-000000000000 secondID:00000000-0000-0000-0000-000000000000
Original: mainID:{00000000-0000-0000-0000-000000000000} secondID:00000000-0000-0000-0000-000000000000
Original: mainID:{00000000-0000-0000-0000-000000000000} secondID:30303030-3030-3030-2d30-3030302d3030 <--
Original: mainID:{00000000-0000-0000-0000-000000000000} secondID:00000000-0000-0000-0000-000000000000
Original: mainID:{00000000-0000-0000-0000-000000000000} secondID:00000000-0000-0000-0000-000000000000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants
@illusionare @go-jet @houten11 and others