Created
October 28, 2013 16:06
-
-
Save shrkw/7199636 to your computer and use it in GitHub Desktop.
ForeingKey experiments on SQLAlchemy.
It seems following 2 python codes are equivalent.
According to my experiments, if you want to have `relationship`, you need to use explicitly any `foreign` by `ForeignKey` schema or `foreign` annotation.
Anyway you can get the power of `relationship` with or without the foreign key constraints.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE addresses ( | |
id INTEGER NOT NULL, | |
email VARCHAR NOT NULL, | |
owner_id INTEGER, | |
PRIMARY KEY (id) | |
); | |
CREATE TABLE users ( | |
id INTEGER NOT NULL, | |
name VARCHAR NOT NULL, | |
PRIMARY KEY (id) | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy.schema import Column, ForeignKey | |
from sqlalchemy.types import Integer, String | |
from sqlalchemy.orm import relation | |
from sqlalchemy.engine import create_engine | |
from sqlalchemy.orm.session import sessionmaker | |
from sqlalchemy.orm import foreign | |
Base = declarative_base() | |
class User(Base): | |
__tablename__ = 'users' | |
id = Column(Integer, primary_key=True) | |
name = Column(String, nullable=False) | |
addresses = relation('Address', primaryjoin='foreign(Address.owner_id) == User.id') | |
class Address(Base): | |
__tablename__ = 'addresses' | |
id = Column(Integer, primary_key=True) | |
email = Column(String, nullable=False) | |
owner_id = Column(Integer) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy.schema import Column, ForeignKey | |
from sqlalchemy.types import Integer, String | |
from sqlalchemy.orm import relation | |
from sqlalchemy.engine import create_engine | |
from sqlalchemy.orm.session import sessionmaker | |
Base = declarative_base() | |
class User(Base): | |
__tablename__ = 'users' | |
id = Column(Integer, primary_key=True) | |
name = Column(String, nullable=False) | |
addresses = relation('Address') | |
class Address(Base): | |
__tablename__ = 'addresses' | |
id = Column(Integer, primary_key=True) | |
email = Column(String, nullable=False) | |
owner_id = Column(Integer, ForeignKey('users.id')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment