Skip to content

missing schema name in generated SQL for foreign keys #2464

Closed

Description

@setthase

Notice: I'm using PostgreSQL with schemas, and sequelizer 2.0-rc2

I've got a small problem trying sequelize do all magic with my relations.
I want to create relationship n:m, but I also want that transition table to have few additional fields.
I've defined model with explicit foreign keys (where references are pointing to model instance) - something like this:

// db = new Sequelize(...);

db.define('company_view', {

  sort_order   : { type : Sequelize.INTEGER },

  company_code : {
    type          : Sequelize.STRING,
    primaryKey    : true,
    references    : db.models.company,
    referencesKey : "code"
  }, 

  view_code : {
    type          : Sequelize.STRING,
    primaryKey    : true,
    references    : db.models.view,
    referencesKey : "code",
  } 

}, { schema : 'configuration' })

But when I try to create schema I've got this SQL - which do not have schema names in references bits:

CREATE TABLE IF NOT EXISTS "configuration"."company_views" (
  "sort_order" INTEGER, 
  "company_code" VARCHAR(255)  REFERENCES "companies" ("code"),  -- look ma' - no schema
  "view_code" VARCHAR(255)  REFERENCES "views" ("code"),         -- look ma' - no schema
  "created_at" TIMESTAMP WITH TIME ZONE NOT NULL, 
  "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL, 
  PRIMARY KEY ("company_code","view_code")
);

Changing definition to something like this also doesn't work:

  // ...
  view_code : {
    type          : Sequelize.STRING,
    primaryKey    : true,
    references    : '"configuration"."views"',
    referencesKey : "code",
  } 
  // ...

Any ideas for workaround until it's fixed?

Activity

  1. added
    type: bug
    on Oct 22, 2014
  2. changed the title [-][bug] missing schema name in generated SQL for foreign keys[/-] [+]missing schema name in generated SQL for foreign keys[/+] on Oct 22, 2014
  3. setthase commented on Oct 23, 2014

    @setthase
    Author

    I think I found a solution to this issue! We need to change this lines :

          if (attribute.references instanceof Model) {
            attribute.references = attribute.references.tableName;
          }

    to look like this:

          if (attribute.references instanceof Model) {
            attribute.references = attribute.references.getTableName();
          }
  4. janmeier commented on Oct 23, 2014

    @janmeier
    Member

    Yep, that sounds plausible. Feel free to provide a PR with that code + test :)

  5. setthase commented on Oct 23, 2014

    @setthase
    Author

    Maybe we should grep source to find all .tableName and change it to .getTableName()?

  6. mickhansen commented on Oct 23, 2014

    @mickhansen
    Contributor

    @codename- not a bad idea, in general it should be using getTableName() since that handles the schema stuff as you figured out.

  7. mickhansen commented on Oct 23, 2014

    @mickhansen
    Contributor

    @janmeier kinda strange we change the tablename in the model, that should probably not happen untill SQL generation, oh well :p

  8. janmeier commented on Oct 23, 2014

    @janmeier
    Member

    Yeah doing it in query generation would probably be cleaner :)

  9. iamjoyce commented on Aug 31, 2016

    @iamjoyce

    This issue also exists for MSSQL. The fix suggested by @codename- does solve the problem.

  10. felixfbecker commented on Aug 31, 2016

    @felixfbecker
    Contributor

    @iamjoyce PR welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    staletype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions