denodb icon indicating copy to clipboard operation
denodb copied to clipboard

Many-to-many model creation

Open saydus opened this issue 3 years ago • 2 comments

The documentation says that pivot table models should be added first, but it's not working this way for me.

Models:

class Question extends Model {
    static table = 'questions';
    static timestamps = true;
    static fields = {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        content: DataTypes.TEXT,
        specificity: DataTypes.enum(["general", "operations", "development", "hacker experience", "design", "sponsorship", "content", "marketing"])
    };

    static notes() {
         return this.hasMany(Note);
    }
}

class Note extends Model {
    static table = 'notes';
    static timestamps = true;
    static fields = {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        interviewer_name: DataTypes.STRING,
        reliability: DataTypes.enum([1, 2, 3, 4, 5, 6, 7]),
        interest: DataTypes.enum([1, 2, 3, 4, 5, 6, 7]),
        teamwork: DataTypes.enum([1, 2, 3, 4, 5, 6, 7]),
        overall: DataTypes.enum([1, 2, 3, 4, 5, 6, 7]),
        thoughts: DataTypes.TEXT
    };

    
    static questions(){
        return this.hasMany(Question);
    }
}

const QuestionNote = Relationships.manyToMany(Question, Note);

The error I am getting:

error: Uncaught (in promise) PostgresError: relation "questions" does not exist
  return new PostgresError(errorFields);
         ^
    at parseError (error.ts:106:10)
    at Connection._processError (connection.ts:434:19)
    at Connection._simpleQuery (connection.ts:340:22)
    at async Connection.query (connection.ts:546:16)
    at async Client.query (client.ts:25:12)
    at async PostgresConnector.query (postgres-connector.ts:60:22)
    at async Database.query (database.ts:164:21)
    at async Function.createTable (model.ts:161:5)
    at async Database.sync (database.ts:134:7)
    at async app.ts:15:1

Changing the order and deleting static questions() helped get rid of the issue.

saydus avatar Dec 28 '20 17:12 saydus

Hit this error as well. Could not find a unit test for the many-to-many case.

pcj avatar Apr 10 '22 20:04 pcj

same... for me putting the pivot model last in the list seems to work


db.link([User, Invoice, UserInvoice])
await db.sync({drop: true})

mrmos avatar May 05 '22 04:05 mrmos