Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 防止SQLAlchemy更改具有两列外键的关联上的字段_Python_Postgresql_Sqlalchemy_Foreign Keys - Fatal编程技术网

Python 防止SQLAlchemy更改具有两列外键的关联上的字段

Python 防止SQLAlchemy更改具有两列外键的关联上的字段,python,postgresql,sqlalchemy,foreign-keys,Python,Postgresql,Sqlalchemy,Foreign Keys,我在PostgreSQL 9.2中使用SQLAlchemy 0.8.4 我有三个简单的模型:帐户、地址和信用卡。地址和信用卡都有必需的帐户关联。地址具有可选的信用卡关联。我正试图强制执行一个约束,这样,只有当他们拥有相同的帐户时,信用卡才能附加到一个地址 我遇到的问题是,当我试图将地址与具有不同帐户的信用卡相关联时,SQLAlchemy没有引发整数错误,而是默默地更改地址的帐户id以匹配信用卡的帐户id 以下是模型: class CreditCard(Base): __tablename

我在PostgreSQL 9.2中使用SQLAlchemy 0.8.4

我有三个简单的模型:帐户、地址和信用卡。地址和信用卡都有必需的帐户关联。地址具有可选的信用卡关联。我正试图强制执行一个约束,这样,只有当他们拥有相同的帐户时,信用卡才能附加到一个地址

我遇到的问题是,当我试图将地址与具有不同帐户的信用卡相关联时,SQLAlchemy没有引发整数错误,而是默默地更改地址的帐户id以匹配信用卡的帐户id

以下是模型:

class CreditCard(Base):
    __tablename__ = 'credit_card'

    id = Column('id', INTEGER(), nullable=False, primary_key=True)
    account_id = Column('account_id', INTEGER(), ForeignKey('account.id'), nullable=False)
    card_number = Column('card_number', TEXT(), nullable=False)

    account = relationship("Account")


class Address(Base):
    __tablename__ = 'address'

    id = Column('id', INTEGER(), nullable=False, primary_key=True)
    account_id = Column('account_id', INTEGER(), ForeignKey('account.id'), nullable=False)
    credit_card_id = Column('credit_card_id', INTEGER(), nullable=True)

    __table_args__ = (
        ForeignKeyConstraint(['credit_card_id', 'account_id'], ['credit_card.id', 'credit_card.account_id']),
    )

    account = relationship("Account")
    credit_card = relationship("CreditCard")
ForeignKeyConstraint与数据库中的多列约束匹配:

CREATE TABLE address (
    id SERIAL NOT NULL UNIQUE,
    account_id INTEGER NOT NULL REFERENCES account(id),
    credit_card_id INTEGER NULL,
    FOREIGN KEY (credit_card_id, account_id) REFERENCES credit_card(id, account_id)
);
account1 = Account(name="account 1")
db.add(account1)
cc = CreditCard(account=account1, card_number="1234")
db.add(cc)
account2 = Account(name="account 2")
db.add(account2)
address = Address(account=account2)
db.add(address)
db.commit()

print "before cc.account_id = %d" % cc.account_id
print "before address.account_id = %d" % address.account_id

address.credit_card = cc
db.add(address)
db.commit()

print "after cc.account_id = %d" % cc.account_id
print "after address.account_id = %d" % address.account_id
以下是与数据库交互的代码:

CREATE TABLE address (
    id SERIAL NOT NULL UNIQUE,
    account_id INTEGER NOT NULL REFERENCES account(id),
    credit_card_id INTEGER NULL,
    FOREIGN KEY (credit_card_id, account_id) REFERENCES credit_card(id, account_id)
);
account1 = Account(name="account 1")
db.add(account1)
cc = CreditCard(account=account1, card_number="1234")
db.add(cc)
account2 = Account(name="account 2")
db.add(account2)
address = Address(account=account2)
db.add(address)
db.commit()

print "before cc.account_id = %d" % cc.account_id
print "before address.account_id = %d" % address.account_id

address.credit_card = cc
db.add(address)
db.commit()

print "after cc.account_id = %d" % cc.account_id
print "after address.account_id = %d" % address.account_id
运行时,它输出:

before cc.account_id = 13
before address.account_id = 14
after cc.account_id = 13
after address.account_id = 13
这是一个和一个

我所期望和希望的是,尝试分配address.credit_card=cc将引发IntegrityError或其他一些失败。相反,它只是在幕后更改了地址的帐户id

这真是不直观的行为。有人知道如何预防吗