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

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

Open
kopy011 opened this issue Jun 3, 2024 · 0 comments

Comments

@kopy011
Copy link

kopy011 commented Jun 3, 2024

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

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