relationship

Two has_many links between the same models

白昼怎懂夜的黑 提交于 2019-12-04 02:15:49
问题 I have users which have products through a habtm link, which is working. I want to add a link between the user model and the product model, to keep track of the creator of that product (who doesn't always own the product, of course) But when I write in my user and product models a new link, the application screws up because I can't distinguish the creator of a product from the owner of (a lot of) products . Can you help me ? Here is my models : class Product < ActiveRecord::Base belongs_to

Many-to-many self-referential relationship in sqlalchemy

本秂侑毒 提交于 2019-12-04 00:26:29
I'm trying to make a self-referential many-to-many relationship (it means that Line can have many parent lines and many child lines) in sqlalchemy like this: Base = declarative_base() class Association(Base): __tablename__ = 'association' prev_id = Column(Integer, ForeignKey('line.id'), primary_key=True) next_id = Column(Integer, ForeignKey('line.id'), primary_key=True) class Line(Base): __tablename__ = 'line' id = Column(Integer, primary_key = True) text = Column(Text) condition = Column(Text) action = Column(Text) next_lines = relationship(Association, backref="prev_lines") class Root(Base):

Rails: How do I model preferences? Should I use has_many :through?

二次信任 提交于 2019-12-03 22:58:36
I have two models: User and HairColor. A user has only one hair color, but can have many hair color preferences. What's the best way to model this? Here's what I started to do: #models/user.rb class User < ActiveRecord::Base belongs_to :hair_color has_many :preferences, has_many :hair_colors, :through => :preferences end #models/hair_color.rb class HairColor < ActiveRecord::Base has_many :users end #models/preference.rb class Preference < ActiveRecord::Base belongs_to :user belongs_to :hair_color end Is using has_many :through the right approach? Also what if I want to extend this to other

Reload association/related collection in NHibernate

偶尔善良 提交于 2019-12-03 21:39:33
If I have Order entity with a list of OrderDetails I can easily eager load the detail along with the order by using NHibernateUtil.Initialize(Order.Details). So obviously the NHibernate have all the information to generate the sql statement. But how do I query the database for just the Details (similar to CreateSourceQuery in Entity Framework) without manually creating a criteria? Is there something like NHibernateUtil.GetList(Order.Details)? Update: Using Darin's answer this what I finally ended up with. This is generic enough i can implement it in entity base class Dim entity as EntityBase

Django 1.8 - Intermediary Many-to-Many-Through Relationship - What is the consequence of where 'ManytoManyField' is used?

纵饮孤独 提交于 2019-12-03 17:30:36
问题 An example Many-to-Many through relationship in Django: class First(models.Model): seconds = models.ManyToManyField(Second, through='Middle') class Middle(models.Model): first = models.ForeignKey(First) second = models.ForeignKey(Second) class Second(models.Model): Following the documentation on intermediary models, only one model of the pair to be related contains the ManytoManyField , model First in the example above. Is this correct? If so, which model should contain the ManytoManyField

How to define two relationships to the same table in SQLAlchemy

对着背影说爱祢 提交于 2019-12-03 15:06:29
问题 I’ve looked all over the SQLAlchemy tutorial and other similar questions but I seem to be struggling to get this join to work: The scenario : I have a pages table represented by the Page model. Pages can be created by an user and edited by an user, but not necessarily the same one. My Page model looks like this (abridged): class Page(Base): __tablename__ = 'pages' id = Column(Integer, primary_key = True) slug = Column(Text) title = Column(Text) direct_link = Column(Text) body = Column(Text)

SQLAlchemy import tables with relationships

旧时模样 提交于 2019-12-03 12:02:47
问题 I have problem with separating tables with relationships in different files. I want the tables below to be in three separate files and to import TableA in third party page, but I can not manage the load order. In most of the time I'm receiving the following error. sqlalchemy.exc. InvalidRequestError: When initializing mapper Mapper|TableA|tablea, expression 'TableB' failed to locate a name ("name 'TableB' is not defined"). If this is a class name, consider adding this relationship() to the

neo4j: one-directional / two-directional relationships?

自古美人都是妖i 提交于 2019-12-03 11:10:57
So I looked into neo4j, and I may be using it in an upcoming project since its data model might fit my project very well. I looked through the docs but I still need an answer to this question: Can I set relationships to be one-directional? It seems that the neo4j people like movies so let's continue with that. If I have a graph like this: Actor A -> [:Acts in] -> Movie B then direction is obvious, because the nodes are different types. But I like horror movies so... Person A -> [:wants_to_kill] -> Person B I need this relationship to be one-directional so if I query "Who does Person A want to

SQLAlchemy Declarative + relationships across multiple different databases

爱⌒轻易说出口 提交于 2019-12-03 09:05:10
问题 It took me a while, but I figured out how to use SQLAlchemy to model a relationship across two different kinds of databases: Base = declarative_base() class Survey(Base): __tablename__ = 'SURVEY' survey_id = Column("SURVEY_ID", Integer, primary_key=True) term_id = Column("TERM_ID", Integer, nullable=False) # Because the TERM table is in Oracle, but the SURVEY table is in # MySQL, I can't rely on SQLAlchemy's ForeignKey. Thus, # I need to specify the relationship entirely by hand, like so:

How to find its owner DataGrid and DataGridRow from DataGridCell in WPF?

半腔热情 提交于 2019-12-03 08:58:30
问题 In an event handler for a Command for a DataGrid, I get DataGridCell in ExecutedRoutedEventArgs. However, I couldn't figure out how to get its associated DataGrid and DataGridRow. Your help is much appreciated. 回答1: You probably want to set some sort of RelativeSource binding that can get you the "parent grid/row" via a {RelativeSource FindAncestor, AncestorType={x:Type DataGrid}} , but your question got me thinking... You could: Use Reflection: var gridCell = ....; var parentRow = gridCell