Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Associations keys not replaced by include #1701

Closed
1 of 2 tasks
M1000fr opened this issue Nov 28, 2023 · 0 comments
Closed
1 of 2 tasks

Associations keys not replaced by include #1701

M1000fr opened this issue Nov 28, 2023 · 0 comments

Comments

@M1000fr
Copy link

M1000fr commented Nov 28, 2023

Issue

When i try to get a ModelA include ModelB the key of the association is undefined but in VALUE.dataValues.ASSOCIATION i've the values

Example:

User Table

import { Column, HasMany, Model, Table } from "sequelize-typescript";
import { Post } from "./Post";

@Table
export class User extends Model<User> {
	@Column({
		allowNull: false,
		unique: true,
		primaryKey: true,
	})
	declare id: number;

	@HasMany(() => Post, { as: "CreatedPosts" })
	CreatedPosts: Post[];
}

Post Table

import {
	BelongsTo,
	Column,
	ForeignKey,
	Model,
	Table,
} from "sequelize-typescript";
import { User } from "./User";

@Table
export class Post extends Model<Post> {
	@Column({
		allowNull: false,
		unique: true,
		primaryKey: true,
	})
	declare id: number;

	@ForeignKey(() => User)
	@Column
	declare userId: number;

	@BelongsTo(() => User) user: User;
}

Try to get user with association post

const sequelize = new Sequelize({
	dialect: "sqlite",
	storage: "database.sqlite",
	logging: false,
	sync: { alter: true },
	define: {
		timestamps: false,
		underscored: true,
	},
});

sequelize.addModels([Models.User, Models.Post]);

sequelize.sync().then(() => {
	Models.User.findByPk(1, {
		include: [Models.Post],
		plain: true,
	}).then((user) => {
		console.log(user); // User Object
		console.log(user.CreatedPosts); // Undefined
		console.log(user.dataValues.CreatedPosts); // Array of User Posts
	});
});

return

User {
  dataValues: { id: 1, CreatedPosts: [ [Post] ] },
  _previousDataValues: { id: 1, CreatedPosts: [ [Post] ] },
  uniqno: 1,
  _changed: Set(0) {},
  _options: {
    isNewRecord: false,
    _schema: null,
    _schemaDelimiter: '',
    include: [ [Object] ],
    includeNames: [ 'CreatedPosts' ],
    includeMap: { CreatedPosts: [Object] },
    includeValidated: true,
    attributes: [ 'id' ],
    raw: true
  },
  isNewRecord: false,
  CreatedPosts: undefined
}
undefined
[
  Post {
    dataValues: { id: 12, userId: 1 },
    _previousDataValues: { id: 12, userId: 1 },
    uniqno: 1,
    _changed: Set(0) {},
    _options: {
      isNewRecord: false,
      _schema: null,
      _schemaDelimiter: '',
      include: undefined,
      includeNames: undefined,
      includeMap: undefined,
      includeValidated: true,
      raw: true,
      attributes: undefined
    },
    isNewRecord: false,
    user: undefined
  }
]

Versions

  • sequelize: ^6.35.1
  • sequelize-typescript: ^2.1.6
  • typescript: 4.9.5

Issue type

  • bug report
  • feature request
@M1000fr M1000fr closed this as completed Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant