我有以下型号:
class Look(Base):
__tablename__ = "looks"
id = Column(Integer, primary_key=True)
url = Column(String, nullable=False, unique=True)
class Similarity(Base):
__tablename__ = 'similarities'
__table_args__ = (UniqueConstraint('look_id_small', 'look_id_big'),)
id = Column(Integer, primary_key=True)
look_id_small = Column(Integer, ForeignKey('looks.id'), nullable=False)
look_id_big = Column(Integer, ForeignKey('looks.id'), nullable=False)
当我运行此代码时:
try:
with session.begin_nested():
similarity = Similarity()
similarity.look_id_small, similarity.look_id_big = look_id1, look_id2
session.add(similarity)
session.commit()
except Exception, e:
logging.error(e)
print "look_id1: %s, look_id2: %s" % (look_id1, look_id2)
这是我得到的错误:
2013-01-19 04:55:42,974 ERROR Foreign key associated with column 'similarities.look_id_small' could not find table 'looks' with which to generate a foreign key to target column 'id'
look_id1: 217137, look_id2: 283579
所以我尝试在 pgsql 中寻找这些值,它们确实存在!
giordano=# SELECT * FROM looks WHERE id = 217137 or id = 283579;
id | url | title | image_url |
--------+-----------------------------------------------+-------+-----------+
217137 | http://foo.com | | |
283579 | http://baz.com | | |
(2 rows)
我整晚都在试图弄清楚这一点。
一些线索:
- 我只在某些值上得到这些错误。
- 我不认为在同一张桌子上有双外键会导致问题。
任何人?
编辑:
giordano=# \d+ looks
Table "public.looks"
Column | Type | Modifiers | Storage | Description
---------------+-------------------+----------------------------------------------------+----------+-------------
id | integer | not null default nextval('looks_id_seq'::regclass) | plain |
url | character varying | not null | extended |
Indexes:
"looks_pkey" PRIMARY KEY, btree (id)
"looks_url_key" UNIQUE CONSTRAINT, btree (url)
Referenced by:
TABLE "similarities" CONSTRAINT "similarities_look_id_big_fkey" FOREIGN KEY (look_id_big) REFERENCES looks(id)
TABLE "similarities" CONSTRAINT "similarities_look_id_small_fkey" FOREIGN KEY (look_id_small) REFERENCES looks(id)
Has OIDs: no
giordano=# \d+ similarities
Table "public.similarities"
Column | Type | Modifiers | Storage | Description
---------------+------------------+-----------------------------------------------------------+---------+-------------
id | integer | not null default nextval('similarities_id_seq'::regclass) | plain |
look_id_small | integer | not null | plain |
look_id_big | integer | not null | plain |
Indexes:
"similarities_pkey" PRIMARY KEY, btree (id)
"similarities_look_id_small_look_id_big_key" UNIQUE CONSTRAINT, btree (look_id_small, look_id_big)
Foreign-key constraints:
"similarities_look_id_big_fkey" FOREIGN KEY (look_id_big) REFERENCES looks(id)
"similarities_look_id_small_fkey" FOREIGN KEY (look_id_small) REFERENCES looks(id)
Has OIDs: no
编辑
打开我的 postgresql 语句后,这就是我所看到的:
LOG: statement: BEGIN
LOG: statement: select version()
LOG: statement: select current_schema()
LOG: statement: show transaction isolation level
LOG: statement: SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1
LOG: statement: SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1
LOG: statement: ROLLBACK
LOG: statement: BEGIN
LOG: statement: DECLARE "c_10dfc08d0_1L" CURSOR WITHOUT HOLD FOR SELECT feedbacks.id AS feedbacks_id, feedbacks.user_id AS feedbacks_user_id, feedbacks.look_id AS feedbacks_look_id
FROM feedbacks
LIMIT 500
LOG: statement: FETCH FORWARD 1 FROM "c_10dfc08d0_1L"
LOG: statement: FETCH FORWARD 5 FROM "c_10dfc08d0_1L"
LOG: statement: FETCH FORWARD 10 FROM "c_10dfc08d0_1L"
LOG: statement: FETCH FORWARD 20 FROM "c_10dfc08d0_1L"
LOG: statement: FETCH FORWARD 50 FROM "c_10dfc08d0_1L"
LOG: statement: FETCH FORWARD 100 FROM "c_10dfc08d0_1L"
LOG: statement: FETCH FORWARD 250 FROM "c_10dfc08d0_1L"
LOG: statement: FETCH FORWARD 500 FROM "c_10dfc08d0_1L"
LOG: statement: FETCH FORWARD 1000 FROM "c_10dfc08d0_1L"
LOG: statement: CLOSE "c_10dfc08d0_1L"
LOG: statement: ROLLBACK
LOG: unexpected EOF on client connection
我没有看到任何“插入”。为什么?