Hi,
I am using Rails Fixtures to load some test data to my database and accidentally I introduced a foreign key out of range.
To my surprise, the database accepted it despite having referential integrity constraints (that work). I tried with PostgreSQL and with MySQL InnoDB and both allowed.
Example:
Having in the database "Flavours" whith a numeric primary key (id), 5 entries (1 to 5). I can introduce bad data doing:
Icecream_1: name: my ice cream flavour_id: 6
How is it possible that the fixtures loading go around my database constraints?
Thank you.
Here are two tables. Having 200 user_types (fake data) I was able to introduce a user with user_type_id 201 but only from fixtures, pgAdmin forbids it.
CREATE SEQUENCE user_types_id_seq;
CREATE TABLE user_types (
id SMALLINT
NOT NULL
DEFAULT NEXTVAL('user_types_id_seq'),
name VARCHAR(45)
NOT NULL
UNIQUE,
PRIMARY KEY (id));
CREATE SEQUENCE users_id_seq;
CREATE TABLE users (
id BIGINT
NOT NULL
DEFAULT NEXTVAL('users_id_seq'),
user_type_id SMALLINT
NOT NULL
REFERENCES user_types (id) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY (id));
---------
Fixture
<% for i in (1..201) %>
user_<%= i %>:
id: <%= i %>
user_type_id: <%= i %>
<% end %>
And as I said, both innoDb and postgresql accepted the bad key.
Thanks