sqlboiler icon indicating copy to clipboard operation
sqlboiler copied to clipboard

unable to insert record into sqlite3 db

Open bluebrown opened this issue 2 years ago • 4 comments

The version is: SQLBoiler v4.11.0 and I am using the sqlite3 driver.

I have this handler, and I can't get it working. I have tried all variations of columns like whitelist and blacklist and so on, but I think boil.Infer() should be actually correct.

When I enable debugMode I get the following logs:

id: 0, title: Learn Go, completed: false
INSERT INTO "todo" DEFAULT VALUES RETURNING "id","completed"
[]
2022/04/30 19:13:03 error: models: unable to insert into todo: id

The code procuding is the below. I am posting a json object with a tilte to this handler..

func (a *Application) TodoCreate() httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		// decode the todo item
		var todo models.Todo
		if err := json.NewDecoder(r.Body).Decode(&todo); err != nil {
			log.Printf("error: %v", err)
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}

		// ensure the todo item has a title
		if todo.Title == "" {
			http.Error(w, "title required", http.StatusBadRequest)
			return
		}

		// debug
		fmt.Printf("id: %d, title: %s, completed: %t\n", todo.ID, todo.Title, todo.Completed)

		// insert the item
		if err := todo.Insert(r.Context(), a.DB, boil.Infer()); err != nil {
			log.Printf("error: %v", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		// return the newly created item
		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusCreated)
		json.NewEncoder(w).Encode(todo)
	}

It's also suspicious that in the above logs, it didn't even try to insert the title which is set on the object.

The SQL schema looks like this:

CREATE TABLE if not exists todo (
  id integer PRIMARY KEY AUTOINCREMENT NOT NULL,
  title text NOT NULL,
  completed boolean NOT NULL DEFAULT false
);

This is the request I am making:

POST /todos HTTP/1.1
Host: localhost:8080
Content-Type: application/json

{"title": "Learn Go"}

Am I using it wrong or is this some kind of bug?

bluebrown avatar Apr 30 '22 19:04 bluebrown

This looks like a bug for sure.

Can you try if this issue persists with v4.10.2?

stephenafamo avatar May 01 '22 20:05 stephenafamo

Hi, thanks for getting back. I just checked with v4.10.2, and it's the same.

bluebrown avatar May 01 '22 21:05 bluebrown

Alright. Sorry about this. I will check in a couple of weeks as I am unable to check at this time.

Can you make sure you're using the SQLite driver in this main repo and no longer the driver that has a separate repository?

stephenafamo avatar May 06 '22 05:05 stephenafamo

@bluebrown I think your issue is related to a problem with AUTOINCREMENT tables. Already opened a PR for this @stephenafamo : https://github.com/volatiletech/sqlboiler/pull/1130

@bluebrown As a workaround, try to remove "completeted" from the todoGeneratedColumns in your generated models/todo.go file.

pbedat avatar May 08 '22 09:05 pbedat