ModelForm with OneToOneField in Django

You have to create second form for PrinterAddress and handle both forms in you view: if all((profile_form.is_valid(), address_form.is_valid())): profile = profile_form.save() address = address_form.save(commit=False) address.printer_profile = profile address.save() Of course in the template you need to show both forms under one <form> tag 🙂 <form action=”” method=”post”> {% csrf_token %} {{ profile_form }} {{ address_form … Read more

Java: Hibernate @OneToOne mapping

Your Status entity must not have properties userId and contentId of type Integer, mapped with @Column. It must have properties user and content of type User and Content, mapped with @OneToOne: public class User { @OneToOne(mappedBy = “user”) private Status status; // … } public class Status { @OneToOne @JoinColumn(name = “frn_user_id”) private User user; … Read more

How to declare one to one relationship using Entity Framework 4 Code First (POCO)

Three methods: A) Declare both classes with navigation properties to each other. Mark one of the tables (the dependent table) with the ForeignKey attribute on its Primary Key. EF infers 1-to-1 from this: public class AppUser { public int Id { get; set; } public string Username { get; set; } public OpenIdInfo OpenIdInfo { … Read more

Why use a 1-to-1 relationship in database design?

From the logical standpoint, a 1:1 relationship should always be merged into a single table. On the other hand, there may be physical considerations for such “vertical partitioning” or “row splitting”, especially if you know you’ll access some columns more frequently or in different pattern than the others, for example: You might want to cluster … Read more

JPA @OneToOne with Shared ID — Can I do this Better?

To map one-to-one association using shared primary keys use @PrimaryKeyJoinColumn and @MapsId annotation. Relevant sections of the Hibernate Reference Documentation: PrimaryKeyJoinColumn The PrimaryKeyJoinColumn annotation does say that the primary key of the entity is used as the foreign key value to the associated entity. MapsId The MapsId annotation ask Hibernate to copy the identifier from … Read more