One to One self-referentianship - Wrong mapping order











up vote
0
down vote

favorite












I've got one sqlalchemy model, declared in such way:



class Cat(BaseModel):  # BaseModel declares id and uuid field, also renames table to 'cats'
name = db.Column(db.String(256))
color= db.Column(db.String(256))

next_cat_id = db.Column(db.Integer(), db.ForeignKey('cats.id'))
next_cat = db.relationship("Cat", uselist=False)


So it should One-One rel. where previous cat points at the next one.
I prepared some dummy data in my postgres table:



id  next_cat_id 
1 2
2 3
3 4
4 5
5 Null


Problem comes out when I try to get object of the next cat:



cat = Cat.query.filter_by(id=5).first()  # Gives cat with id=5 (OK)
> cat.next_cat_id # Gives None (OK)
> cat.next_cat # Gives cat with id=4 instead of None


I thing sqlalchemy have some troubles with resolving relationship direction. I tried also to specify remote_side parameter, but still same problem occurs.










share|improve this question
























  • setting remote_side on next_cat fixed it for me: next_cat = db.relationship("Cat", uselist=False, remote_side=[id])
    – SuperShoot
    yesterday















up vote
0
down vote

favorite












I've got one sqlalchemy model, declared in such way:



class Cat(BaseModel):  # BaseModel declares id and uuid field, also renames table to 'cats'
name = db.Column(db.String(256))
color= db.Column(db.String(256))

next_cat_id = db.Column(db.Integer(), db.ForeignKey('cats.id'))
next_cat = db.relationship("Cat", uselist=False)


So it should One-One rel. where previous cat points at the next one.
I prepared some dummy data in my postgres table:



id  next_cat_id 
1 2
2 3
3 4
4 5
5 Null


Problem comes out when I try to get object of the next cat:



cat = Cat.query.filter_by(id=5).first()  # Gives cat with id=5 (OK)
> cat.next_cat_id # Gives None (OK)
> cat.next_cat # Gives cat with id=4 instead of None


I thing sqlalchemy have some troubles with resolving relationship direction. I tried also to specify remote_side parameter, but still same problem occurs.










share|improve this question
























  • setting remote_side on next_cat fixed it for me: next_cat = db.relationship("Cat", uselist=False, remote_side=[id])
    – SuperShoot
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've got one sqlalchemy model, declared in such way:



class Cat(BaseModel):  # BaseModel declares id and uuid field, also renames table to 'cats'
name = db.Column(db.String(256))
color= db.Column(db.String(256))

next_cat_id = db.Column(db.Integer(), db.ForeignKey('cats.id'))
next_cat = db.relationship("Cat", uselist=False)


So it should One-One rel. where previous cat points at the next one.
I prepared some dummy data in my postgres table:



id  next_cat_id 
1 2
2 3
3 4
4 5
5 Null


Problem comes out when I try to get object of the next cat:



cat = Cat.query.filter_by(id=5).first()  # Gives cat with id=5 (OK)
> cat.next_cat_id # Gives None (OK)
> cat.next_cat # Gives cat with id=4 instead of None


I thing sqlalchemy have some troubles with resolving relationship direction. I tried also to specify remote_side parameter, but still same problem occurs.










share|improve this question















I've got one sqlalchemy model, declared in such way:



class Cat(BaseModel):  # BaseModel declares id and uuid field, also renames table to 'cats'
name = db.Column(db.String(256))
color= db.Column(db.String(256))

next_cat_id = db.Column(db.Integer(), db.ForeignKey('cats.id'))
next_cat = db.relationship("Cat", uselist=False)


So it should One-One rel. where previous cat points at the next one.
I prepared some dummy data in my postgres table:



id  next_cat_id 
1 2
2 3
3 4
4 5
5 Null


Problem comes out when I try to get object of the next cat:



cat = Cat.query.filter_by(id=5).first()  # Gives cat with id=5 (OK)
> cat.next_cat_id # Gives None (OK)
> cat.next_cat # Gives cat with id=4 instead of None


I thing sqlalchemy have some troubles with resolving relationship direction. I tried also to specify remote_side parameter, but still same problem occurs.







python postgresql sqlalchemy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Ilja Everilä

22.2k33359




22.2k33359










asked yesterday









needtobe

12917




12917












  • setting remote_side on next_cat fixed it for me: next_cat = db.relationship("Cat", uselist=False, remote_side=[id])
    – SuperShoot
    yesterday


















  • setting remote_side on next_cat fixed it for me: next_cat = db.relationship("Cat", uselist=False, remote_side=[id])
    – SuperShoot
    yesterday
















setting remote_side on next_cat fixed it for me: next_cat = db.relationship("Cat", uselist=False, remote_side=[id])
– SuperShoot
yesterday




setting remote_side on next_cat fixed it for me: next_cat = db.relationship("Cat", uselist=False, remote_side=[id])
– SuperShoot
yesterday

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203606%2fone-to-one-self-referentianship-wrong-mapping-order%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203606%2fone-to-one-self-referentianship-wrong-mapping-order%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff