Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlalchemy complains that foreign key doesn't exist but actually it exists

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?

like image 389
disappearedng Avatar asked Jan 19 '13 13:01

disappearedng


1 Answers

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?

like image 102
disappearedng Avatar answered Oct 05 '22 11:10

disappearedng