On Feb 14, 2014, at 3:16 PM, Michael Hipp <mich...@redmule.com> wrote:

> On 2/14/2014 1:51 PM, Michael Bayer wrote:
>> right this is why reading the docs is better, those have been checked...  
>> remote side for m2o refers to the primary key, so:
> 
> The docs says: '...directive is added known as remote_side, which is a Column 
> or collection of Column objects that indicate those which should be 
> considered to be "remote":'
> 
> Nothing there particularly hints that it must be the primary key.

The document I linked earlier is:

http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html#adjacency-list-relationships

The section includes an in-depth explanation of what “remote_side” means, 
followed by a code example of many to one which then says below it "thus 
establishing parent_id as the “local” side, and the relationship then behaves 
as a many-to-one."

I am adding “see also” crosslinks to the “remote side” documentation now.

> 
> This is working great until I add in this line:
>  a1.sire = a1
> Gives exception:
> 
> sqlalchemy.exc.CircularDependencyError: Circular dependency detected. Cycles: 
> set([ProcessState(ManyToOneDP(Animal.sire), <Animal at 0x33f13b0>, 
> delete=False), SaveUpdateState(<Animal at 0x33f13b0>)]) all edges: 
> set([(SaveUpdateState(<Animal at 0x348b970>), 
> ProcessState(ManyToOneDP(Animal.dam), <Animal at 0x33fa430>, delete=False)), 
> (SaveUpdateState(<Animal at 0x33f13b0>), 
> ProcessState(ManyToOneDP(Animal.sire), <Animal at 0x33f13b0>, delete=False))
> ...
> 
> No bull ever sired himself. But the first object I intended to populate in 
> the db would be the "unknown" sire who would just have unknown (himself) as a 
> sire. The 'sire' column needs to be not nullable or I'd just leave it blank.

A basic fact of a self referential relationship is that you’re building a tree. 
 The root of the tree has to be NULL and I’d advise against trying to work 
around that.

Now if you wanted to in fact assign the object’s own primary key to the foreign 
key column, you can (manually), but even then, you’d need to know the primary 
key up front.  If you’re relying on the database to generate a primary key 
value, you still won’t have that value in time, and the column would still be 
NULL.

I think where you’re at with this you might want to keep it simple to start.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to