Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create one-to-one relationships with declarative

Tags:

sqlalchemy

I have two tables, foo and bar, and I want foo.bar_id to link to bar. The catch is that this is a one-way one-to-one relationship. bar must not know anything about foo. For every foo, there will be one and only one bar.

Ideally, after selecting a foo, I could do something like this:

myfoo.bar.whatever = 5 

How to accomplish this?

like image 262
carl Avatar asked Aug 12 '10 02:08

carl


People also ask

What is an example of a one-to-one relationship?

Here are some examples of one-to-one relationships in the home: One family lives in one house, and the house contains one family. One person has one passport, and the passport can only be used by one person. One person has one ID number, and the ID number is unique to one person.

How do I create a one-to-one relationship in mysql?

One to One Relationship (1:1) When a row in a table is related to only one role in another table and vice versa,we say that is a one to one relationship. This relationship can be created using Primary key-Unique foreign key constraints.

How do you create a one to many relationship in Python?

The comments class attribute defines a One-to-Many relationship between the Post model and the Comment model. You use the db. relationship() method, passing it the name of the comments model ( Comment in this case). You use the backref parameter to add a back reference that behaves like a column to the Comment model.


3 Answers

The documentation explains this nicely:

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    child = relationship("Child", uselist=False, backref="parent")

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))

OR

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    child_id = Column(Integer, ForeignKey('child.id'))
    child = relationship("Child", backref=backref("parent", uselist=False))

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
like image 82
chadwick.boulay Avatar answered Oct 18 '22 10:10

chadwick.boulay


If you want a true one-to-one relationship, you also have to use the "uselist=False" in your relationship definition.

bar_id = Column(Integer, ForeignKey(Bar.id))
bar = relationship(Bar, uselist=False)
like image 25
Brett Bim Avatar answered Oct 18 '22 11:10

Brett Bim


I think if it is a truly one to one relationship we should add a uniqueness constraint to foreign key so another parent can not have other parent child!! Like this:

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    child_id = Column(Integer, ForeignKey('child.id'), unique=True)
    child = relationship("Child", backref=backref("parent", uselist=False))

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
like image 23
hamidfzm Avatar answered Oct 18 '22 11:10

hamidfzm