insert with InferModel not working

Hey, playing with Drizzle, like it a lot but I am having an issue where I inferred the model of a schema as type NewUser = InferModel<typeof Users, "insert">; and try to insert. It doesn't give an error, simply does nothing. In my app I have it do many different selects so I know the schema is fine and everything is connected and working. Yet when I execute this insert nothing is added to the db.
import { InferModel } from "drizzle-orm";

type NewUser = InferModel<typeof Users, "insert">;

const insertUser = async (userNew: NewUser) => {
return db.insert(Users).values(userNew)
}

async function main() {
let userNew : NewUser = {
email: "xxx@xxx.net",
password: "xxx",
token: "xxx"
};
console.log(userNew);

await insertUser(userNew);
}

main();
import { InferModel } from "drizzle-orm";

type NewUser = InferModel<typeof Users, "insert">;

const insertUser = async (userNew: NewUser) => {
return db.insert(Users).values(userNew)
}

async function main() {
let userNew : NewUser = {
email: "xxx@xxx.net",
password: "xxx",
token: "xxx"
};
console.log(userNew);

await insertUser(userNew);
}

main();
Here is the table schema (sqlite):
export const Users = sqliteTable("users", {
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
email: text("email").notNull(),
password: text("password").notNull(),
token: text("token"),
createdAt: integer("createdAt", { mode: "timestamp" }).notNull().defaultNow(),
updatedAt: integer("updatedAt", { mode: "timestamp" }).notNull().defaultNow(),
})
export const Users = sqliteTable("users", {
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
email: text("email").notNull(),
password: text("password").notNull(),
token: text("token"),
createdAt: integer("createdAt", { mode: "timestamp" }).notNull().defaultNow(),
updatedAt: integer("updatedAt", { mode: "timestamp" }).notNull().defaultNow(),
})
3 Replies
Frosty
Frosty15mo ago
Little bit of an update, drizzle won't write to the table at all. It can read it but not write. I removed the timestamp fields it still won't work. The only thing left to try is to remove the autoIncrement attribute The Studio app can write to the db Its not permissions related, gave everyone/thing permission to read/write Think this is a bug in drizzle itself at this point
Frosty
Frosty15mo ago
had to add .run() to the end of the inserts and it works now https://github.com/drizzle-team/drizzle-orm/issues/815
GitHub
[BUG]: Undocumented - SQLite requires .run() to commit inserts · ...
What version of drizzle-orm are you using? 0.27.0 What version of drizzle-kit are you using? 0.19.2 Describe the Bug Running an .insert while using SQLite3 (via better-sqlite3) doesn't do anyth...
Frosty
Frosty15mo ago
- inserts won't run or produce errors if you don't include .run() - inserts won't return data if you don't include .all() - inserts won't run if they don't include returning data and you called it with .all() instead of .run()
const newUser = (userData: NewUser) => {
return db.insert(Users).values(userData).returning({id: Users.id}).all()
}

const newTodo = (todoData: NewTodo) => {
return db.insert(Todos).values(todoData).run()
}

const create = async () => {
let result1 = await newUser(<NewUser>{
email: "testuser@test.net",
password: "12345",
})

// result1 will be [ { id: 1 } ]

await newTodo(<NewTodo>{
owner: result1[0].id,
title: "Test Todo",
notes: "This is a test todo",
completed: false,
})
}
const newUser = (userData: NewUser) => {
return db.insert(Users).values(userData).returning({id: Users.id}).all()
}

const newTodo = (todoData: NewTodo) => {
return db.insert(Todos).values(todoData).run()
}

const create = async () => {
let result1 = await newUser(<NewUser>{
email: "testuser@test.net",
password: "12345",
})

// result1 will be [ { id: 1 } ]

await newTodo(<NewTodo>{
owner: result1[0].id,
title: "Test Todo",
notes: "This is a test todo",
completed: false,
})
}
Want results from more Discord servers?
Add your server