简体   繁体   中英

Foreign key Constraint on delete cascade does not work postgres

When I run DELETE FROM users WHERE id='some_id' the record on beta_keys table does not get deleted.

beta_keys table:

CREATE TABLE beta_keys (
    id serial PRIMARY KEY,
    key VARCHAR(60) UNIQUE NOT NULL,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP
);

users table:

CREATE TABLE users (
    id serial PRIMARY KEY,
    email VARCHAR (256) UNIQUE NOT NULL,
    password VARCHAR (60) NOT NULL,
    beta_key_id INTEGER,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP,
    CONSTRAINT users_beta_key_id_fkey FOREIGN KEY (beta_key_id)
        REFERENCES beta_keys (id) MATCH SIMPLE
        ON DELETE CASCADE
);

users references beta_keys . delete cascade works by deleting referencing rows ( users ) when the referenced row ( beta_keys ) is deleted.

sqlfiddle: http://sqlfiddle.com/#!17/a7495/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM