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

setting two foreign keys to associate with the same field, deleting data is normal but does not take effect #2671

Closed
Mickls opened this issue Sep 20, 2024 · 6 comments · Fixed by #2704
Assignees

Comments

@Mickls
Copy link

Mickls commented Sep 20, 2024

I created a simple script to reproduce the problem

func main() {
	db := memory.NewDatabase("testdb")
	db.BaseDatabase.EnablePrimaryKeyIndexes()
	pro := memory.NewDBProvider(db)
	session := memory.NewSession(sql.NewBaseSession(), pro)
	ctx := sql.NewContext(context.Background(), sql.WithSession(session))
	engine := sqle.NewDefault(pro)
	createTableRow := `CREATE TABLE testdb.table_row (
        id INT PRIMARY KEY AUTO_INCREMENT,
        name VARCHAR(255) NOT NULL
    );`
	createTableRelation := `CREATE TABLE testdb.table_relation (
        id INT PRIMARY KEY AUTO_INCREMENT,
        source_row_id INT,
        target_row_id INT,
        CONSTRAINT table_relations_table_rows_source_relations FOREIGN KEY (source_row_id) REFERENCES testdb.table_row(id) ON DELETE CASCADE,
        CONSTRAINT table_relations_table_rows_target_relations FOREIGN KEY (target_row_id) REFERENCES testdb.table_row(id) ON DELETE CASCADE
    );`
	_, _, err := engine.Query(ctx, createTableRow)
	if err != nil {
		log.Fatal(err)
	}
	_, _, err = engine.Query(ctx, createTableRelation)
	if err != nil {
		log.Fatal(err)
	}
	insertData := `INSERT INTO testdb.table_row (name) VALUES ('row1'), ('row2'), ('row3');`
	_, row, err := engine.Query(ctx, insertData)
	if err != nil {
		log.Fatal(err)
	}
	r, err := row.Next(ctx)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(r)
	insertRelation := `INSERT INTO testdb.table_relation (source_row_id, target_row_id) VALUES (1, 2), (2, 3);`
	_, relation, err := engine.Query(ctx, insertRelation)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(relation.Next(ctx))
	deleteData := `DELETE FROM testdb.table_row WHERE id = 1;`
	_, d, err := engine.Query(ctx, deleteData)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(d.Next(ctx))
	_, rows, err := engine.Query(ctx, "SELECT * FROM testdb.table_row LIMIT 10;")
	if err != nil {
		log.Fatal(err)
	}
	rs, err := rows.Next(ctx)
	fmt.Println(rs)
	rs, err = rows.Next(ctx)
	fmt.Println(rs)
	rs, err = rows.Next(ctx)
	fmt.Println(rs)
}

The above code performs the following operations: Create tables table_row and table_relation. The table table_relation contains source_row_id and target_row_id and both are foreign keys to the id field of table_row. Then insert data into the table and delete the data in table_row.
As can be seen from the printed output, we want to delete the row with id 1. The SQL statement runs without exception, but the deletion is not successful.

@zachmu
Copy link
Member

zachmu commented Oct 14, 2024

HI @Mickls:

Sorry for the delayed response here.

I've tested this and it does seem to be a bug. We'll get it on our docket. Thanks for the report!

@Mickls
Copy link
Author

Mickls commented Oct 15, 2024

HI @Mickls:

Sorry for the delayed response here.

I've tested this and it does seem to be a bug. We'll get it on our docket. Thanks for the report!

I'm glad this issue has been responded to. In fact, it has caused many of my UTs to not run correctly, and I had to switch to other solutions. I would be very grateful if this problem can be solved.

@timsehn
Copy link
Contributor

timsehn commented Oct 15, 2024

If this is a bug in Dolt as well, we'll move it to that queue and fix it in the next couple days. If not, it will be fixed in the next week or so.

@jycor
Copy link
Contributor

jycor commented Oct 16, 2024

Hey @Mickls, thanks for reporting this issue!
I have a fix currently out for review, and we will let you know once it's merged.

Fortunately, this issue was somehow only apparent in GMS and not dolt.

@jycor
Copy link
Contributor

jycor commented Oct 16, 2024

Hey @Mickls, this has been fixed and merged to GMS main!
Let us know how it works for you.

@Mickls
Copy link
Author

Mickls commented Oct 17, 2024

Hey @Mickls, this has been fixed and merged to GMS main! Let us know how it works for you.

I tested the merged code and it works fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants