How insert one-to-many

There is a database with two tables (recipe, flavor). Between them one-to-many relationships one recipe is a lot of flavors.
I add data through the put vue method

  saverecipe: function(){
                    this.$http.put('/recipe/new', this.newrecipe).then(function (data) {                                    
                        console.log(this.newrecipe)
                    })
                }  

Google Chrome data example with one flavor

handler

func PutRecipe(db *sql.DB) echo.HandlerFunc {
	return func(c echo.Context) error {

		var recipe models.Recipe
		c.Bind(&recipe)

		id, err := models.PutRecipe(db, recipe.RECIPENAME, recipe.BOTTLEID, recipe.BOTTLESIZE, recipe.PG, recipe.VG, recipe.NICOTINE,
			recipe.DATE, recipe.NOTE, recipe.FLAVORID, recipe.NAME, recipe.DROPS, recipe.RECIPEID)

		if err == nil {
			return c.JSON(http.StatusCreated, H{
				"created": id,
			})

		} else {
			log.Fatal(err)
			return err
		}
	}
}

model

func PutRecipe(db *sql.DB, recipename string, bottleID int, bottlesize int, pg int, vg int, nicotine int, date string, note string, flavorid int, name string,
	drops int, recipeid int) (int64, error) {
	sql := `INSERT INTO recipe(recipename, bottleID, bottlesize, pg, vg, nicotine, date, note, flavorid) VALUES(?,?,?,?,?,?,?,?,?,?,?,?);
	INSERT INTO flavor(name, drops, recipeid) VALUES(?,?,?)`

	stmt, err := db.Prepare(sql)
	if err != nil {
		log.Fatal(err)
	}

	defer stmt.Close()

	result, err2 := stmt.Exec(recipename, bottleID, bottlesize, pg, vg, nicotine, date, note, flavorid, name, drops, recipeid)

	if err2 != nil {
		log.Fatal(err2)
	}

	return result.LastInsertId()
}

Data must be written in two tables.
One is a recipe.
One - flavor.
How can I do that?
If you need I can lay out all the code on the github

Your sql string always writes one recipe and one flavor. If you need to insert multiple flavors per recipe, you need to split the two INSERTs into different strings and stmt.Exec() statements.

Then you can insert one recipe through the one Exec, and all flavors related to this recipe through the other Exec in a loop.

And you need to have a recpieid column in both tables, in order to match flavors with a particular receipe. From the INSERT statement it would look as if the recipe table has no column to store its own ID. But I may be wrong, it could be a system-provided column as well.

I’m getting an error: 12 values for 9 columns

     `INSERT INTO recipe(recipename, bottleID, bottlesize, pg, vg, nicotine, date, note, flavorid) VALUES(?,?,?,?,?,?,?,?,?,?,?,?);

Where I transfer not 12 and 9 values. If you pass 12 values then also an error there is no column
Do at least to work with one favor
recpieid == flavorid

The number of question marks does not match with the number of column names. You have nine column names but 12 question marks.

Already changed everything, but error again: expected 9 arguments, got 12

Did you also adjust the stmt.Exec() call from 12 to 9 values?

result, err2 := stmt.Exec(recipename, bottleID, bottlesize, pg, vg, nicotine, date, note, flavorid)

error

Exception 0xc0000005 0x8 0xa382f8 0xa382f8
PC=0xa382f8
signal arrived during external code execution

Would you mind sharing the complete PutRecipe function of the model?

Also, does the error message come with some more context (e.g., which signal was received)

If I correctly understood you then all functions are in the first post. Here’s what outputs to the console

⇨ http server started on [::]:8000
Exception 0xc0000005 0x8 0xa382f8 0xa382f8
PC=0xa382f8
signal arrived during external code execution
github.com/mattn/go-sqlite3._Cfunc__sqlite3_step(0x340adf8, 0xc042134ca0, 0xc042134ca8, 0x0)
github.com/mattn/go-sqlite3/_obj/_cgo_gotypes.go:241 +0x54
 github.com/mattn/go-sqlite3.(*SQLiteStmt).exec.func4(0x340adf8, 0xc042134ca0, 0xc042134ca8, 0xc0420421e0)
/github.com/mattn/go-sqlite3/sqlite3.go:968 +0x7b
 github.com/mattn/go-sqlite3.(*SQLiteStmt).exec(0xc0421325a0, 0x3692a0, 0xc0420421e0, 0xc042164180, 0x9, 0x9, 0x0, 0x0, 0x0, 0x0)
github.com/mattn/go-sqlite3/sqlite3.go:968 +0x1a2
 github.com/mattn/go-sqlite3.(*SQLiteStmt).ExecContext(0xc0421325a0, 0xa3f4e0, 0xc0420421e0, 0xc042164000, 0x9, 0x9, 0xc000000008, 0xc04204a2c0, 0xc042031578, 0x5916ac)
github.com/mattn/go-sqlite3/sqlite3_go18.go:68 +0x239

github

I’m not sure if this is your actual problem, but you prepare a statement with 12 “holes”:

But you pass in only 9 values when executing the statement:

I tried differently, I get errors. Just on the github, my last option

What have you tried? Have you actually tried to provide the additional 3 values necessary? Thats the flavors name, drops and reipeid what is missing in line 91 of the snippet you linked to, as I showed in my previous post.

result, err2 := stmt.Exec(recipename, bottleID, bottlesize, pg, vg, nicotine, date, note, flavorid,name, drops, recipeid)

error - expected 9 arguments, got 12. I already wrote about this in the message above

Since providing more values doesn’t work you probably have to reduce the number of holes in your statement… Delete the second line…

Edit

You are not allowed to have multiple statements in a single prepare according to my SQL manual

https://dev.mysql.com/doc/refman/5.7/en/prepare.html

I do not have a mysql database, I use sqlite. I also receive errors when deleting the second line. I tried to add 9 values- the error is described above.

Have you tried my suggestion? I have to assume that similar restrictions or worse apply on sqlite…

I using

So you are still using 2 statements in the prepared one. Just do as you was told and remove the second statement from sql and then report back that it worked…

Error

Exception 0xc0000005 0x8 0xa382f8 0xa382f8
PC=0xa382f8
signal arrived during external code execution

Do not pay much attention to examples of github.I gave the github to show the complete code.
You can use my code on your pc, for testing

THen please provide the steps necessary to reproduce your bug. I do not get any message of failure nor success on inserting, but on the index the newly inserted item is displayed as empty, when I click it, I do get an error in the console that is unrelated to your current problem…