Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hasMany sync() wrong column datatypes #510

Closed
snovak7 opened this issue May 26, 2014 · 1 comment
Closed

hasMany sync() wrong column datatypes #510

snovak7 opened this issue May 26, 2014 · 1 comment
Labels

Comments

@snovak7
Copy link

snovak7 commented May 26, 2014

When I set custom key to a model, and make a hasMany relationship, the underlying table is incorrect. tried with version (excerpt from package.json -> "orm": "~2.1.10")

var Email = persistence.define('email', {
    id: {type: 'text', key: true},
    bounced: Boolean,
    last_checked: Date,
}); 

var Account = persistence.define('account', {
    name: String
});

Account.hasMany('email', Email); // or
Email.hasMany('account', Account);

I get these DDL commands for base tables

[SQL/postgres] CREATE TABLE "email" ("id" TEXT, "bounced" BOOLEAN, "last_checked" DATE, PRIMARY KEY ("id"))
[SQL/postgres] CREATE TABLE "account" ("name" TEXT, "id" SERIAL, "parent_id" INTEGER, "account_type_id" INTEGER, PRIMARY KEY ("id"))

and many to many table

[SQL/postgres] CREATE TABLE "account_email" ("account_id" TEXT, "email_id" INTEGER, PRIMARY KEY ("account_id"))

Two observations which I noticed is, that it reverses the datatype, and only one is primary key, both should be primary keys... no matter what datatype.

@dxg dxg added the bug label May 27, 2014
dxg added a commit that referenced this issue May 27, 2014
@dxg
Copy link
Collaborator

dxg commented May 27, 2014

I've fixed the problem. It appears the issue was present for a long time.

It's on master - please test it by changing your package.json to:

"orm": "git://github.com/dresende/node-orm2.git#master",

As for primary keys - the single primary key was indeed a bug. By default ORM never created keys for join tables. Whilst this is somewhat unexpected behaviour, changing it might break things for people.

To get a composite primary key, you can define the association like so:

Account.hasMany('email', Email, {}, { key: true });

@snovak7 snovak7 closed this as completed May 27, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants