sequelize 설정 및 사용

김기태·2021년 10월 12일
0
  1. npm i sequelize sequelize-cli mysql2

  2. config 수정 : logging:false 하면 로그가 안보인다.

  3. npx sequelize db:create -- db생성

  4. npx sequelize model:generate --name User --attributes email:string,nickname:string,password:string -- 모델 생성

  5. model과 migration 파일 수정(고유ID를 id로 저장하는데 우리는 다른 ID를 사용하기 위함)

  6. model file의 id값에 primaryKey: true을 준다.

  7. 이후 migrationfile의 Id를 수정.

  8. npx sequelize db:migrate - sequelize table생성.

  9. 관계있는 db끼리 associate 맺어주기
    ex)
    this.belongsTo(models.users, { foreignKey: 'authorID' })
    this.hasMany(models.postings, { foreignKey: 'authorID' });

  10. 관계를 맺어서 정보를 가져올 땐

코드를 입력하include: [
  //     {
  //       model: users,
  //       attributes: ['nick'],
  //     }
profile
김개발

0개의 댓글