profile
viewpoint

Ask questionsRelation does not exist (Foregin Key)

<!-- Before submitting an issue, please consult our docs (https://github.com/RobinBuschmann/sequelize-typescript) and check whether your problem was already reported (https://github.com/RobinBuschmann/sequelize-typescript/issues) -->

<!-- Please make sure you are posting an issue pertaining to sequelize-typescript. If you are having an issue with sequelize itself, please consult the sequelize team (https://github.com/sequelize/sequelize/issues) -->

Versions <!-- Please let us know which sequelize, sequelize-typescript and typescript version are you using -->

  • sequelize: 5.10.2
  • sequelize-typescript: 1.0.0-beta.3
  • typescript 3.5.3

I'm submitting a ... <!-- (check one with "x") --> [x] bug report [ ] feature request

Actual behavior: <!-- Describe how the bug manifests. --> First time using sequelize and sequelize-typescriot. I have two models, device and measurement. A device can hav many measurements. A measurement is from exactly one device.

The Table "Devices" is created. The Table measurement can't be created. The error is that the relation "Devices" does not exist.

Executing (default): CREATE TABLE IF NOT EXISTS "test"."Measurements" ("id"   SERIAL , "deviceType" VARCHAR(255), "deviceId" INTEGER REFERENCES "Devices" ("id") ON DELETE CASCADE ON UPDATE CASCADE, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id"));
sequelize.js:1182
(node:14524) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: Relation »Devices« does not exist

It looks like the schema "test" is missing in the SQL. After INTEGER REFERENCES it should be "test"."Devices" shouldn't it?

Expected behavior: Create the table :)

Steps to reproduce: <!-- Please explain the steps required to duplicate the issue, especially if you are able to provide a sample application. -->

Related code:

@Table
export class Device extends Model<Device> 
{
    @Column
    euid: string;

    @Column
    type: String;

    @HasMany(() => Measurement)
    measurements: Measurement[];

}
@Table
export class Measurement extends Model<Measurement> 
{

    @Column
    deviceType: string;

    @ForeignKey(() => Device)
    @Column
    deviceId: number;
}

Main

async function preparePostgres()
{
    const sequelize =  new Sequelize({
        database: 'xxx',
        host: "localhost",
        port: 5432,
        dialect: 'postgres',
        username: 'xxx',
        password: 'xxx'
    });

    sequelize.addModels([Device, Measurement]);

    let schemas : any[] = await sequelize.showAllSchemas({});
    if (!schemas.includes("test"))
    {
        await sequelize.createSchema("test", {});
    }
    await sequelize.sync({schema: 'test'});
    

    return sequelize;
}

preparePostgres().then((sequelize) => {
    let test = Device.build({euid: "haha", type: "asdf"});
    test.save();
});
RobinBuschmann/sequelize-typescript

Answer questions RobinBuschmann

Closing this due to inactivity

useful!
source:https://uonfu.com/
Github User Rank List