Skip to content

hasMany relation doesn't work #2

Description

@Hoo93

Here is my example factory code


import { defineFactory } from '@julr/factorify';

interface Member {
  createId:string
  username:string
  password:string
  name:string
  refreshToken:string
  id:string
  type:string
  loginType:string
  nickname:string
  mobileNumber:string
  email:string
  birthYear:string
  birthday:string
  isAutoLogin:boolean
}

// it has memberId column which is FK of member.id
interface LoginHsitory {
  id:number
  currentIp:string
}


export const LoginHistoryFacotry = defineFactory<LoginHsitory>('member_login_history', ({ faker, isStubbed }) => ({
  id: +faker.random.numeric(4),
  currentIp:faker.random.alpha(6),
}))
  .build()


export const MemberFactory = defineFactory<Member>('member', ({ faker, isStubbed }) => ({
  createId: faker.random.alpha(6),
  username: faker.random.alpha(6),
  password: faker.random.alpha(6),
  name: faker.random.alpha(6),
  refreshToken: faker.random.alpha(6),
  id: faker.random.alpha(6),
  type: faker.random.alpha(6),
  loginType: faker.random.alpha(6),
  nickname: faker.random.alpha(6),
  mobileNumber: faker.random.alpha(6),
  email: faker.random.alpha(6),
  birthYear: faker.random.alpha(6),
  birthday: faker.random.alpha(6),
  isAutoLogin: true,
}))
  .hasMany('loginHistories',() => LoginHistoryFacotry,{
    localKey: 'id',
    foreignKey: 'memberId'
  })
  .build()

and this is my usage code

import { MemberFactory } from '../defineFactory';
import { defineFactorifyConfig } from '@julr/factorify';

describe('MemberFactory Test', () => {
  let disconnect:any;
  beforeAll(() => {
    disconnect = defineFactorifyConfig({
      casing:{
        insert:'camel',
        return:'camel'
      },
      database: {
        // See https://knexjs.org/guide/#configuration-options
        // for more information about the possible options
        client: 'mysql',
        connection: {
          host: '127.0.0.1',
          port: 8922,
          user: 'root',
          password: 'test',
          database: 'eco_test',
        },
      },
    })
  })
  

  it('MemberFactory With LoginHistory Test', async () => {
    // Given
    const user = await MemberFactory.with('loginHistories',1).create()
    
  });

});

But await MemberFactory.with('loginHistories',1).create() invoke error below.

insert into member_login_history (currentIp, id) values ('qswwnh', 7921) - ER_NO_DEFAULT_FOR_FIELD: Field 'memberId' doesn't have a default value Error: ER_NO_DEFAULT_FOR_FIELD: Field 'memberId' doesn't have a default value

My member_login_history Table looks like this
image

How can I make await MemberFactory.with('loginHistories',1).create() work ?

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions