Very helpful, thanks Tim :)

On Monday, August 12, 2013 9:53:48 PM UTC-4, Tim wrote:
>
>  Annnnd, one more try: 
>
> existing_creator = DBSession.query(Creator).filter_by(name=creator).first()
>
> -- 
> Tim Van Steenburgh
>
> On Monday, August 12, 2013 at 9:50 PM, Tim Van Steenburgh wrote:
>
>  Sorry, that should have been: 
>
> existing_creator = 
> DBSession(Creator).query.filter_by(creator=creator).first()
>
> On Monday, August 12, 2013 at 9:49 PM, Tim Van Steenburgh wrote:
>
>  It's not the append that's causing the error, it's the fact that you're 
> creating a new Creator() instance, which ultimately results in an INSERT 
> statement being issued. 
>
> You want to append a Creator instance to `company.creator`, but you don't 
> necessarily want to make a new Creator every time you instantiate a 
> Company. If a Creator with the given name already exists, you'll want use 
> that instead.
>
> So, roughly:
>
> class Company(Base):
>     __tablename__ = "companies"
>     id = Column(Integer, primary_key = True)
>     company = Column(String(100), unique=True, nullable=False)
>     creator = relationship("Creator", backref="companies", cascade="all")
>     def __init__(self, company, creator):
>         self.company = company
>   existing_creator = 
> DBSession(Creator).query.filter_by(name=creator).first()
>         self.creator.append(existing_creator or Creator(creator))
>
> -- 
> Tim Van Steenburgh
>
>
> On Monday, August 12, 2013 at 9:41 PM, csd...@gmail.com <javascript:>wrote:
>
> Sorry I don't understand what you're trying to say. 
>
> If the Creator already exists, and I'm to append it again, isn't that the 
> same as what my code is currently doing? (That is, appending in every 
> instance.) I don't see how this wouldn't result in the same error message.
>
> And what would it mean to create a new one and append that? I don't know 
> what this code would look like.
>
> Apologies if I'm being dense.
>
> On Monday, August 12, 2013 9:33:31 PM UTC-4, Tim wrote:
>
> In `Company.__init__()`, instead of blindly creating a new `Creator` 
> instance, you need to first query for an existing Creator with that name. 
> If it exists, append it, otherwise, create a new one and append that. 
>
> -- 
> Tim Van Steenburgh
>
> On Monday, August 12, 2013 at 9:26 PM, csd...@gmail.com wrote:
>
> I have another question about a piece of code that I posted the other day. 
> Namely, I have a one-to-many relationship between Creator and Company. A 
> Creator can have a relationship with multiple Companies but any one Company 
> can have a relationship with only one Creator.
>
> class Company(Base):
>     __tablename__ = "companies"
>     id = Column(Integer, primary_key = True)
>     company = Column(String(100), unique=True, nullable=False)
>     creator = relationship("Creator", backref="companies", cascade="all")
>     def __init__(self, company, creator):
>         self.company = company
>         self.creator.append(Creator(creator))
>
> class Creator(Base):
>     __tablename__ = "creators"
>     company_id = Column(Integer, ForeignKey('companies.id'))
>     creator = Column(String(100), nullable=False, unique=True)
>     def __init__(self, creator):
>         self.creator = creator
>
> So, to create a Company, the code calls company = Company(<company name>, 
> <creator name>) and that in turn calls Creator().
>
> The problem is that the Companies get added one by one, and if a new 
> company being entered has a Creator with a name of a preexisting company, 
> SQLalchemy errors due to the unique=True flag:
>
> sqlalchemy.exc.IntegrityError: (IntegrityError) (1062, "Duplicate entry 
> 'Viking' for key 'creator'") 'INSERT INTO creators (company_id, creator) 
> VALUES (%s, %s)' (17L, u'Viking')
>
> If unique=True isn't enabled, it will create another Creator of the same 
> name. Instead, the code should reflect the additional Company assigned to 
> this particular Creator. How might I go about fixing this?
>
> Thanks!
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+...@googlegroups.com.
> To post to this group, send email to sqlal...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  
>  
>
>   -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+...@googlegroups.com <javascript:>.
> To post to this group, send email to sqlal...@googlegroups.com<javascript:>
> .
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  
>  
>
>   
>   
>  

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to