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.
python postgresql sqlalchemy
add a comment |
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.
python postgresql sqlalchemy
settingremote_side
onnext_cat
fixed it for me:next_cat = db.relationship("Cat", uselist=False, remote_side=[id])
– SuperShoot
yesterday
add a comment |
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.
python postgresql sqlalchemy
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
python postgresql sqlalchemy
edited yesterday
Ilja Everilä
22.2k33359
22.2k33359
asked yesterday
needtobe
12917
12917
settingremote_side
onnext_cat
fixed it for me:next_cat = db.relationship("Cat", uselist=False, remote_side=[id])
– SuperShoot
yesterday
add a comment |
settingremote_side
onnext_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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
setting
remote_side
onnext_cat
fixed it for me:next_cat = db.relationship("Cat", uselist=False, remote_side=[id])
– SuperShoot
yesterday