简体   繁体   English

数据库允许来自Rails Fixtures的错误外键

[英]Databases allow bad foreign keys from Rails Fixtures

I am using Rails Fixtures to load some test data to my database and accidentally I introduced a foreign key out of range. 我正在使用Rails Fixtures将一些测试数据加载到数据库中,并且偶然地引入了超出范围的外键。

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. 我尝试使用PostgreSQL和MySQL InnoDB,并且两者都允许。

Example: 例:

Having in the database "Flavours" whith a numeric primary key (id), 5 entries (1 to 5). 在数据库“风味”中带有数字主键(id)的5个条目(1至5)。 I can introduce bad data doing: 我可以介绍坏数据:

Icecream_1: name: my ice cream flavour_id: 6 Icecream_1:名称:我的冰淇淋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. 拥有200个user_types(伪数据),我能够使用user_type_id 201引入一个用户,但是仅从固定装置引入,pgAdmin禁止这样做。

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. 正如我所说,innoDb和postgresql都接受了错误的密钥。

Thanks 谢谢

PostgreSQL doesn't accept corrupt data, don't worry. PostgreSQL不接受损坏的数据,不用担心。 In MySQL it all depends on the engine (must be innoDB) and the (connection) settings for the parameter foreign_key_checks. 在MySQL中,一切都取决于引擎(必须为innoDB)和参数foreign_key_checks的(连接)设置。

How do your tables and constraints look like? 您的表格和约束看起来如何? Check pgAdmin (or some other client) and dump the relevant piece of datamodel over here, than we can help you out. 检查pgAdmin(或其他客户端)并在此处转储相关的数据模型,我们将为您提供帮助。

pgAdmin forbids it. pgAdmin禁止它。

No, your PostgreSQL database forbids it. 不,您的PostgreSQL数据库禁止这样做。 pgAdmin is just a client and it only sends a query to the database. pgAdmin只是一个客户端,它仅向数据库发送查询。 The database does some checks, FK got violated and returns an error. 数据库进行了一些检查,违反了FK并返回错误。

Looks like you're working on the wrong database (no FK's or MySQL with the wrong engine and/or settings), PostgreSQL works fine when having a FK. 看起来您在使用错误的数据库(没有FK或MySQL的引擎和/或设置错误的数据库),PostgreSQL在拥有FK时可以正常工作。

I agree with Frank. 我同意弗兰克。 Your test database for PostgreSQL is most probably not setup correctly. 您的PostgreSQL测试数据库很可能未正确设置。 You either forgot to create the FK constraints or you disabled them. 您要么忘记创建FK约束,要么禁用它们。

The fact that you got an error in pgAdmin indicates that you are working with a different database from within pgAdmin and your test script. pgAdmin中出现错误的事实表明您正在使用pgAdmin和测试脚本中的其他数据库。

As far as MySQL is concerned I'd look for a wrong default engine in the test database or if you also forgot to create the FK constraints there (note that you will not get an error if you create a FK constraint with an engine that doesn't support referential integrity on MySQL) 就MySQL而言,我会在测试数据库中寻找错误的默认引擎,或者如果您还忘记在该数据库中创建FK约束(请注意,如果使用不包含以下内容的引擎创建FK约束,则不会出错)不支持MySQL上的参照完整性)

Check the table definitions in your test database. 检查测试数据库中的表定义。 IIRC, "rake db:test:prepare" does not maintain fidelity when creating the tables in the test database. IIRC,在测试数据库中创建表时,“ rake db:test:prepare”不保持保真度。

Thank you all for answering. 谢谢大家的回答。

Someone at ruby forum figured it out. 在ruby论坛上有人发现了它。 Looks like the triggers which enforce RI are disabled prior to the loading of the fixtures. 看起来像强制执行RI的触发器在加载夹具之前已被禁用。

I don't know why but it solves the mistery. 我不知道为什么,但是它解决了雾霾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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