How to enforce a one-to-one relationship I want to store data about students and the books they read. Each student can read only one book, and each book can be read by only one student.

I have created a Student content type, and a Book content type, and I have created a reference field in the Student content type to reference the Book content type, and I have made the reference field to only accept one value:



So this way when I am entering data for a student, I can only enter one book value for the student (so the constraint that a student can only read one book is enforced).

But I can still enter the same book for more than one student, for example I can enter book1 for student1 and and I can also enter book1 for student2. How can I prevent this (remember that I want each book to be read by only one student)? The way I can do that if I am creating a database is to make the book foreign key in the Student table unique, but I don't think there is a way to make a field unique in Drupal.
I think you will need to use an entity reference view to enforce your restriction. In the settings for your reference type you can select Default or Views: Filter by an entity reference view

You will need to create your View first, instead of adding a Page or Block you need to add Entity Reference but this isn't available as part of the Views Wizard.

Part of the filter criteria will be to hide books which have already been assigned to a student. Hope this helps you get to your solution