10

I have the following models:

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) 

When I am running this code:

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) 

This is the error I am getting:

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 

so I tried looking for these values in pgsql and they do exist!

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) 

I have spent the whole night trying to figure this out.

Some clues:

  1. I am only getting these errors on certain values.
  2. I don't think having double foreignkeys on the same table will result in an issue.

Anyone?

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

After turning on my postgresql statements, this is what I am seeing:

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 

I am not seeing any "INSERTS". Why?

5
  • The data model looks OK. Try to dig up the generated query (maybe from the postgres log?) Commented Jan 19, 2013 at 13:23
  • Does this help? 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 {} Commented Jan 19, 2013 at 14:12
  • No, that does not help. Try to find the generated SQL query. You may need to crank up your postgres logging level to log all statements (or only the failed ones) Commented Jan 19, 2013 at 14:19
  • Do you mean something like this? codeinthehole.com/writing/configuring-logging-for-postgresapp Commented Jan 19, 2013 at 14:30
  • try setting log_statement = 'all' in your postgres.conf and rerun the query. Commented Jan 19, 2013 at 14:34

1 Answer 1

5

Turns out using ForeignKey(Look.id) did the trick!!

According to argonholm from #sqllachemy:

18:25 agronholm: I would have to guess that the syntax for text-form table references is not what you expected 18:26 agronholm: maybe ForeignKey('Look.id') would also work?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.