python - SQLAlchemy: one to "fixed number" relationship -
lets have basic table setup this:
class car(base): __tablename__ = 'cars' car_id = column(integer, primary_key=true) someotherinfo = column(string) class tire(base): __tablename__ = 'tires' tire_id = column(integer, primary_key=true) car_id = column(integer, foreignkey('car.car_id')) someotherinfo = column(string)
i want relationship between car , tire, such each car has 4 tires associated it. this, add car definition:
tire1 = relationship("tire", uselist=false, backref="car") tire2 = relationship("tire", uselist=false, backref="car") tire3 = relationship("tire", uselist=false, backref="car") tire4 = relationship("tire", uselist=false, backref="car")
but wouldn't allow me iterate on tires (in real problem there more 4 tires). else be:
tires = relationship('tire', backref='car')
but doesn't restrict tires maximum of 4, , doesn't allow me access specific tire (i guess tires list not ordered). want this:
tires = relationship('tire', backref='car', max=4, usearray=true)
where can address tire (array sorted), restrict 4, , able iterate on them.
this first question, hope clear , please correct me if did wrong.
thanks lot!
i solve problem associative table.
class tirecar(base): __tablename__ = 'tire_car_assoc' tire_id = column(integer, primary_key=true) car_id = column(integer, primary_key=true) order = column(integer) someotherinfo = column(string)
where define tire_id
, car_id
foreignkey
of course.
this way can iterate relation or whatever need need do. if can have 4 relations (or n
in real problem) between 2 tables, control in python
.
(the way reason slqalchemy
, or orm
matter, not different database. i'd say, then, design solution mind in database, , implement orm
. maybe don't particularly solution, incomplete, besides have information of functional model! hope helps find own solution.)
Comments
Post a Comment