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

PostgreSQL, relationships - regular column created but there is no foreign key #430

Open
vladimiry opened this issue Jan 11, 2015 · 0 comments

Comments

@vladimiry
Copy link

Code example:

    var schema = new Schema(driverType, connectionConfig);

    var UserToken = schema.define('UserToken', {
        token: {type: String, index: true}
    }, {
        tablename: 'user_token'
    });

    var User = schema.define('User', {
        email: {type: String, allowNull: false, index: true},
        password_hash: String,
        first_name: String,
        last_name: String,
        role: {type: String, allowNull: false, default: 'member'},
        language: {type: String, default: 'en'},
        api_key: String,
        active: {type: Boolean, allowNull: false, default: true},
        confirmed: Date,
        created: {type: Date, default: Date.now},
        modified: Date
    }, {
        tablename: 'users'
    });

    User.hasMany(UserToken, {as: 'tokens', foreignKey: 'userId'});
    UserToken.belongsTo(User, {as: 'user', foreignKey: 'userId'});

    schema.automigrate();

Got DDLs:

CREATE TABLE public."UserToken" (
    id serial NOT NULL DEFAULT nextval('"UserToken_id_seq"'::regclass),
    token varchar,
    "userId" int4,
    CONSTRAINT "UserToken_pkey" PRIMARY KEY (id)
);
CREATE TABLE public."User" (
    id serial NOT NULL DEFAULT nextval('"User_id_seq"'::regclass),
    email varchar NOT NULL,
    password_hash varchar,
    first_name varchar,
    last_name varchar,
    role varchar NOT NULL,
    "language" varchar,
    api_key varchar,
    active bool NOT NULL,
    confirmed timestamptz,
    created timestamptz,
    modified timestamptz,
    CONSTRAINT "User_pkey" PRIMARY KEY (id)
);

So "userId" column created as regular int4, but there is no foreignKey. The use of foreign keys is the basic concept of relational databases, it's required for data integrity.

@vladimiry vladimiry changed the title PostgreSQL, relationships - regular column created instead of foreign key column PostgreSQL, relationships - regular column created but there is no foreign key Jan 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant