Thursday 15 March 2012

postgresql - sqlalchemy, violation foreign key constraint -


I'm new to Skylightmy. I want to create a class that has two foreign keys for different tables, table "Event "Enter or update the foreign key obligation" event_user_fkey "violates the extension: key (user) = (U) The table" user "does not exist in the event (id," user ", item) in the 'event' value ( % (Id) s,% (user) s,% (item) s) '{' item ':' i ',' user ':' y ',' Id ':' e '}

My code is next:

  class user (base): __tablename__ =' user 'id = column (Strings, primary_keys = true) df __int_ (self, id): self.id = id class item (base): __tablename__ = 'item' id = column (strings, primary_keys = true) def __init __ (self, id) : Self. ID = id class event (base): __tablename__ = 'event' id = column (string, primary_key = = true) user = column (string, forex ('user. Id')) item = column (string, foreken ('item. Id ')) def __init __ (self, id, user_id, item_id): self.id = id self.user = user_id self.item = item_id   

I postgresql a back end Use

  base. Metadata.create_all (engine) session = session maker (dam = engine) session = session () USR = user ('U') this = item ('I') event = event ('E', USR.ID, ITID) Session.add (usr) session.add (this) session.add (event)    

T The error looks very clear: key (user) = (U) table is not present in "user".

Therefore it is trying to include the user's first event line, committed to the database, which blocks the obligation of foreigners, causing this error to occur before the event is done. Try to keep the user and the object in the database, depending on their basis and the problem should disappear.

No comments:

Post a Comment