eveningkid / denodb

MySQL, SQLite, MariaDB, PostgreSQL and MongoDB ORM for Deno

Home Page:https://eveningkid.com/denodb-docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"PostgresError: relation does not exist" when syncing

seanaye opened this issue · comments

I have the following two models defined

import { Model, DataTypes, Relationships } from "/deps.ts"

class OrganizationCustomData extends Model {
  static table = "organization_custom_data"
  static timestamps = true

  static updates() {
    return this.hasOne(OrganizationUpdates)
  }

  static fields = {
    id: {
      type: DataTypes.STRING,
      length: 24,
      primaryKey: true,
    },
  }
}

class OrganizationUpdates extends Model {
  static table = "organization_update"
  static timestamps = true

  static organization() {
    return this.hasOne(OrganizationCustomData)
  }

  static fields = {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    desired_version: {
      ...DataTypes.string(32),
      allowNull: false,
      comment: "String semver which the client should update to upon datetime"
    },
    version_url: {
      type: DataTypes.TEXT,
      allowNull: false,
      comment: "A URL link to the update asset"
    },
    should_update_at: {
      type: DataTypes.TIMESTAMP,
      allowNull: false,
      comment: "UTC timestamp at which the client application should update"
    }
  }
}

Relationships.oneToOne(OrganizationCustomData, OrganizationUpdates)
console.log('linked one to one')

export default [OrganizationUpdates, OrganizationCustomData]

And this function to sync to database

import { Database, PostgresConnector } from "/deps.ts";
import models from "/src/models/organizationCustomData.ts"

interface DatabaseConnectionDetails {
  databaseName: string;
  databaseHost: string;
  databaseUsername: string;
  databasePassword: string;
  databasePort: string;
}

export async function syncDB(details: DatabaseConnectionDetails) {
  let port = 5432
  try {
    port = Number(details.databasePort)
  } catch (e) {
    console.warn(`Could not parse string databasePort: "${details.databasePort}" into a number. Defaulting to port 5432`)
  }

  const connector = new PostgresConnector({
    database: details.databaseName,
    host: details.databaseHost,
    username: details.databaseUsername,
    password: details.databasePassword,
    port
  })

  console.log('creating database obj')
  const database = new Database(connector)

  database.link(models)

  console.log('syncing db')
  await database.sync({ drop: true })
  console.log('sync complete')

  return database
}

When I run the application I get the following in my logs

linked one to one
creating database obj
syncing db
Sending fatal alert BadCertificate
TLS connection failed with message: invalid peer certificate contents: invalid peer certificate: UnknownIssuer
Defaulting to non-encrypted connection
error: Uncaught (in promise) PostgresError: relation "organization_custom_data" does not exist
          error = new PostgresError(parseNoticeMessage(current_message));

I have tried with and without {drop: true} same error.

If I remove the Relationships.oneToOne(OrganizationCustomData, OrganizationUpdates) then the tables are created in the db properly

Deno version:

deno 1.19.1 (release, aarch64-apple-darwin)
v8 9.9.115.7
typescript 4.5.2

denodb version: 1.0.40

@seanaye I had (have) the same issue as you. I too have made a models file and was exporting them as a default array - but it turns out this breaks the link function.

The only way I could get it to work is exporting them not as default nor as an array, and putting them in like this : database.link([ Model1, Model2, ... ])

Not very graceful, but works (kinda). Now I have an issue where I have to drop the database every time I restart my server otherwise I get an error about existing foreign key.