sequelize / sequelize-typescript

Decorators and some other features for sequelize
MIT License
2.78k stars 280 forks source link

After webpack confusion, As if the @foreignkey useless #1582

Open Jarcem opened 1 year ago

Jarcem commented 1 year ago

Issue

i am using webpack5 to package the project, when the project runs the error was the primaryKey type is not the same as ForeignKey type, and i checked the final js file. the primaryKey type was the same as the foreignkey type. if i do not useing webpack project runs everything ok there was system report

Referencing column 'items_uuid' and referenced column 'id' in foreign key constraint 'items_wallet_relationship_ibfk_279' are incompatible."

Versions

Issue type

Actual behavior

basic table association relationship

Expected behavior

project runs ok

Steps to reproduce

  1. table one
    @Table({
    tableName: 'items'
    })
    export default class Items extends ModelExtension{
    @Column({
        primaryKey: true
    })
    uuid: string
    @ForeignKey(() => ItemsType)
    @Column({
    })
    type_id: number
    @Column({
    })
    item_name: string
    @Column({
    })
    item_img: string
    @Column({
      defaultValue: 0,
    })
    is_delete : number;
    @BelongsTo(() => ItemsType)
    itemsType: ItemsType
    }
  2. table two

    @Table({
    tableName: 'items_wallet_relationship'
    })
    export default class ItemsWalletRelationship extends ModelExtension{
    @Column({
        comment: 'id',
        autoIncrement: true,
        primaryKey: true
    })
    id: number
    
    @ForeignKey(() => Items)
    @Column({})
    items_uuid: string
    
    @ForeignKey(() => Wallet)
    @Column({})
    wallet_id: number
    
    @BelongsTo(() => Items)
    items: Items
    @BelongsTo(() => Wallet)
    wallet: Wallet
    }

Related code

  1. webpack conf
    
    'use strict';
    const webpack = require('webpack');
    const path = require('path');
    var nodeExternals = require('webpack-node-externals');
    module.exports = {
    entry: {
        app: [
            './dist/server.js'
        ]
    },
    output: {
        path: path.resolve(__dirname, './build'),
        filename: 'server.js'
    },
    target: 'node',
    externals: [nodeExternals()], 
    module: {
        rules: [
            {
                test: /\.js$/,
                use: ['babel-loader']
            }
        ],
    },
    resolve: {
        modules: [
            'node_modules',
            path.resolve(__dirname, 'dist'),
            path.resolve(__dirname, 'node_modules')
        ],
        extensions: [".js"]
    },
    plugins: [
    ]
    };
2. tsconf

{ "compilerOptions": { "module": "commonjs", "esModuleInterop": true, "target": "ES2017", "noImplicitAny": true, "moduleResolution": "node", "sourceMap": true, "outDir": "dist", "baseUrl": ".", "paths": { "": [ "node_modules/", "src/types/*" ] }, "lib": [ "es2017" ], "emitDecoratorMetadata": true, "experimentalDecorators": true, "skipLibCheck": true, "types": ["node", "webpack-env"] }, "include": [ "src/*/" ], "experimentalDecorators": true }


<!-- If you are able to illustrate the bug or feature request with an example, please provide a sample application via one of the following means:

- A sample application via GitHub (Best option, since its much easier for us to investigate, so that we can come back to you more recently)
- A code snippet below (Please make sure, that the snippet at least includes tsconfig and the sequelize options)

-->
yarikpetrenko commented 1 month ago

+1