question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with insert

See original GitHub issue

Issue type:

[X] Question [ ] Bug report [ ] Feature request [ ] Documentation issue

Database system/driver:

[ ] Postgres [ ] MSSQL [X] MySQL [ ] MariaDB [ ] SQLite3 [ ] Oracle [ ] Amazon Redshift

typed-knex version:

[X latest [ ] @next [ ] 0.x.x (or put your version here)

Knex.js version: Latest

Steps to reproduce or a small repository showing the problem:

Hi ! I want to make a simple insertion with two models.

import { Column, Table } from '@wwwouter/typed-knex';

import { Email, UUID } from '../../ts/types/base.type';

@Table('user')
class UserDB {
  @Column({ primary: true })
  public id!: UUID;

  @Column()
  public firstname!: string;

  @Column()
  public lastname!: string;

  @Column()
  public email!: Email;

  @Column()
  public birthdate!: Date;

  @Column()
  public gender: 'M' | 'F' | 'N' = 'N';

  @Column()
  public created_at!: Date;

  @Column()
  public updated_at!: Date;

  @Column()
  public termsofuse: boolean = true;

  @Column()
  public active: boolean = true;
}

export default UserDB;
import { Column, Table } from '@wwwouter/typed-knex';

import User from './User.db.model';
import { Bcrypt, UUID } from '../../ts/types/base.type';

@Table('password')
class PasswordDB {
  @Column({ primary: true })
  public id!: UUID;

  @Column()
  public password!: Bcrypt;

  @Column({ name: 'user_id' })
  public user!: User;

  @Column()
  public created_at!: Date;

  @Column()
  public updated_at!: Date;
}

export default PasswordDB;

I just run :

await db.query(UserDB).insertItem(userDB);
await db.query(GroupDB).insertItem(groupDB);

userDB and groupDB are instances of UserDB and GroupDB. They are full and there is no misfiled attributes.

And I got :

Error: insert into `group` (`group_name`, `user_id`) values (0, {"gender":"N","termsofuse":true,"active":true,"id":"6dc9f9f9-ae34-4eef-8a33-6f514ec05c6e","firstname":"string","lastname":"string","email":"user@example.com","birthdate":"2020-10-08T13:51:50.489Z","created_at":"2020-10-08 16:11:18","updated_at":"2020-10-08 16:11:18"}) - Column count doesn't match value count at row 1
        at Packet.asError (/........node_modules/mysql2/lib/packets/packet.js:712:17)
        at Query.execute (......../node_modules/mysql2/lib/commands/command.js:28:26)
        at Connection.handlePacket (/.........node_modules/mysql2/lib/connection.js:425:32)
        at PacketParser.onPacket (/h........node_modules/mysql2/lib/connection.js:75:12)
        at PacketParser.executeStart (/.........../node_modules/mysql2/lib/packet_parser.js:75:16)
        at Socket.<anonymous> (/............./node_modules/mysql2/lib/connection.js:82:25)
        at Socket.emit (events.js:314:20)
        at Socket.EventEmitter.emit (domain.js:486:12)
        at addChunk (_stream_readable.js:307:12)
        at readableAddChunk (_stream_readable.js:282:9)

What can I do, to avoid to put the user object instead of just user.id ?

Thanks for your work !

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
wwwoutercommented, Oct 17, 2020

Hi,

If you’d log the group object, you should see what’s going wrong.

console.log(group)
 await typedKnex.query(GroupInDB).insertItem(group);

group should only contain the fields you want to insert in the database, so user_id and group_name. Right now the Object.assign puts too much data in the group object.

I think something like this should work:

@Table('group')
class GroupInDB {
  @Column()
  user_id!: string;  // always add the real column

  @Column({ name: 'user_id' })
  user!: UserInDB;

  @Column({ primary: true })
  group_name!: number; // I would keep this type as close to the database type as possible
}
const user = new UserInDB();
    Object.assign(user, {
      id: 'test',
      active: true,
      birthdate: new Date(),
      created_at: new Date(),
      email: 'test@test.com',
      firstname: 'ok',
      gender: GenderType.MALE,
      lastname: 'ok',
      termsofuse: true,
      updated_at: new Date(),
    });

    const group = new GroupInDB();
    Object.assign(group, {
      user_id: user.id,
      group_name: TypeGroup.Student,
    });

    await typedKnex.query(UserInDB).insertItem(user);
    await typedKnex.query(GroupInDB).insertItem(group);
0reactions
wwwoutercommented, Oct 20, 2020

It’s a bcrypt hash, I compare the new hash from the db request !

👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

mysql insert problem - Stack Overflow
ive got a really weird problem. i have no clue why its not working. i create an user and get the id and...
Read more >
SQL INSERT Error – Q&A Hub | 365 Data Science
This error appears if you have already created another table, employees, where you have missed inserting data about the individual with id ...
Read more >
Issue with INSERT data not being available for immediate ...
I've got an issue where it appears that data that I insert isn't available for an immediate SELECT statement executed by the same...
Read more >
Search Insert Position - LeetCode
Search Insert Position - Given a sorted array of distinct integers and a target value, return the index if the target is found....
Read more >
Where is the problem with my insert query? Am connecting to ...
I am trying to insert data into the data base every time the php script is run. The connection to database is establishing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found