eveningkid / denodb

MySQL, SQLite, MariaDB, PostgreSQL and MongoDB ORM for Deno
https://eveningkid.com/denodb-docs
MIT License
1.93k stars 129 forks source link

Many-to-many model creation #167

Open saydus opened 3 years ago

saydus commented 3 years ago

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.

pcj commented 2 years ago

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

mrmos commented 2 years ago

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


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