Skip to content

Sequelize typescript package one-to-many relation inserts foreign key on each sync with alter table #1723

@kopy011

Description

@kopy011

This is the model definition of my User model :

import {
	Table,
	Column,
	Model,
	DataType,
	PrimaryKey,
	AutoIncrement,
	ForeignKey,
	BelongsTo,
	HasMany,
} from 'sequelize-typescript';
import { Role } from './Roles';
import { UserGroupLink } from './UserGroupLink';
import { Validation } from './Validations';

@Table({
	tableName: 'Users',
	timestamps: false,
})
export class User extends Model {
	@PrimaryKey
	@AutoIncrement
	@Column({
		type: DataType.INTEGER.UNSIGNED,
		field: 'ID',
	})
	id!: number;

	@Column({
		type: DataType.STRING,
		allowNull: false,
		field: 'Name',
	})
	email!: string;

	@ForeignKey(() => Role)
	@Column({
		type: DataType.INTEGER.UNSIGNED,
		allowNull: false,
		field: 'RoleID',
		onUpdate: 'CASCADE',
		onDelete: 'CASCADE',
	})
	roleId!: number;

	@BelongsTo(() => Role, {
		foreignKey: 'roleId',
		targetKey: 'id',
		as: 'role',
	})
	role!: Role;

	@HasMany(() => UserGroupLink, {
		foreignKey: 'userId',
		sourceKey: 'id',
		as: 'userGroupLinks',
	})
	userGroupLinks!: UserGroupLink[];

	@HasMany(() => Validation, {
		foreignKey: 'userId',
		sourceKey: 'id',
		as: 'validations',
	})
	validations!: Validation[];
}

and this is the model definition of my Role model :

import { Table, Column, Model, DataType, PrimaryKey, AutoIncrement, HasMany } from 'sequelize-typescript';
import { User } from './Users';

@Table({
	tableName: 'Roles',
	timestamps: false,
})
export class Role extends Model {
	@PrimaryKey
	@AutoIncrement
	@Column({
		type: DataType.INTEGER.UNSIGNED,
		field: 'ID',
	})
	id!: number;

	@Column({
		type: DataType.STRING,
		allowNull: false,
		field: 'Name',
	})
	name!: string;

	@Column({
		type: DataType.TEXT,
		field: 'Description',
	})
	description!: string | null;

	@HasMany(() => User, {
		sourceKey: 'id',
		foreignKey: 'roleId',
		as: 'users',
	})
	users!: User[];
}

Basically what i want to achieve is to make a one to many relation ship between role and user models, one role can have many users but one user can only have one role

This way the relation is correct, i can insert and query for users with role or role with users, the problem is every time the server starts and sequelize.sync({alter: true}) runs, it inserts a new foreign key to users pointing to roles table

enter image description here

According to this issue: "#266"

i've tried modifying my user model like this:

import {
	Table,
	Column,
	Model,
	DataType,
	PrimaryKey,
	AutoIncrement,
	ForeignKey,
	BelongsTo,
	HasMany,
} from 'sequelize-typescript';
import { Role } from './Roles';
import { UserGroupLink } from './UserGroupLink';
import { Validation } from './Validations';

@Table({
	tableName: 'Users',
	timestamps: false,
})
export class User extends Model {
	@PrimaryKey
	@AutoIncrement
	@Column({
		type: DataType.INTEGER.UNSIGNED,
		field: 'ID',
	})
	id!: number;

	@Column({
		type: DataType.STRING,
		allowNull: false,
		field: 'Name',
	})
	email!: string;

	@ForeignKey(() => Role)
	@Column(DataType.INTEGER.UNSIGNED)
	public roleId!: number;

	@BelongsTo(() => Role, {
		foreignKey: 'roleId',
	})
	public role!: Role;

	@HasMany(() => UserGroupLink, {
		foreignKey: 'userId',
		sourceKey: 'id',
		as: 'userGroupLinks',
	})
	userGroupLinks!: UserGroupLink[];

	@HasMany(() => Validation, {
		foreignKey: 'userId',
		sourceKey: 'id',
		as: 'validations',
	})
	validations!: Validation[];
}

but the foreign keys still got inserted

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions