tôi có các mô hình sau:SQLAlchemy than phiền rằng chính nước ngoài không tồn tại nhưng thực sự nó tồn tại
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)
Khi tôi chạy mã này:
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)
Đây là lỗi Tôi nhận được :
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
vì vậy tôi đã thử tìm kiếm các giá trị này trong pgsql và chúng tồn tại!
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)
Tôi đã dành cả đêm để tìm hiểu điều này.
Một số manh mối:
- tôi chỉ nhận được những lỗi trên các giá trị nhất định.
- Tôi không nghĩ rằng việc có đôi phím tắt trên cùng một bảng sẽ dẫn đến sự cố.
Bất kỳ ai?
EDIT:
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
EDIT
Sau khi bật báo cáo postgresql của tôi, đây là những gì tôi đang nhìn thấy:
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
Tôi không thấy bất kỳ "Phụ trang". Tại sao?
Mô hình dữ liệu trông OK. Cố gắng đào lên truy vấn được tạo ra (có thể từ nhật ký postgres?) – wildplasser
Điều này có hữu ích không? ERROR look_id1: 34816, look_id2: 283143 2013-01-19 06: 10: 25,559 INFO sqlalchemy.engine.base.Engine SAVEPOINT sa_savepoint_2 2013-01-19 06: 10: 25,560 INFO sqlalchemy.engine.base.Engine {} 2013-01-19 06: 10: 25,561 INFO sqlalchemy.engine.base.Engine ROLLBACK TO SAVEPOINT sa_savepoint_2 2013-01-19 06: 10: 25,561 INFO sqlalchemy.engine.base.Engine {} – disappearedng
Không, đó không giúp được gì. Hãy thử tìm truy vấn SQL đã tạo. Bạn có thể cần phải tăng cấp đăng nhập của bạn để đăng nhập tất cả các câu lệnh (hoặc chỉ những câu không thành công) – wildplasser