Refers to database tables with a column that is a foreign key to another column within the same table.
Questions tagged [self-referencing-table]
131 questions
1
vote
3 answers
LINQ Self Referencing Table SELECT Query
I have a self referencing employee table, meaning that there are employees with a primary key "EmployeeID" and a foreign key "SupervisorID" for the parent (supervisor). Managers are on top of the hirarchy, so their SupervisorID is null. There's also…

Davatar
- 160
- 1
- 14
1
vote
2 answers
SQLAlchemy FK ondelete does not RESTRICT
I have a self referential relationship established. A person can have one a single parent (or None), and a person can have many children (or None).
So NULL is allowed as a FK:
class Person(db.Model):
id = db.Column(db.Integer,…

Attack68
- 4,437
- 1
- 20
- 40
1
vote
0 answers
How to write a query in SQLAlchemy with a self-referencing table?
I have a self referencing table, that looks like this:
class Domain(Base):
__tablename__ = "domains"
domain_id = Column(Integer(), primary_key=True)
parent_id = Column(Integer(), ForeignKey('domains.domain_id'))
domain_name =…

brainsid
- 13
- 4
1
vote
2 answers
Activerecord has_and_belongs_to_many with nested groups
I need to store groups of users in other groups, somewhat like Windows Active Directory.
I have the following what works
ActiveRecord::Schema.define do
create_table :users do |table|
table.column :name, :string
end
create_table :groups do…

peter
- 41,770
- 5
- 64
- 108
1
vote
1 answer
Make B1 to return A1, B2 to return A2, and so on in script in Google Sheets. What is the correct cell range reference for this function?
This is a very basic question, I almost feel stupid asking it.
I'm writing a function that is supposed to write the data from one cell into a NOTE from another cell in google sheets.
Everything works fine if I test from one single cell to another,…

jcodi
- 39
- 2
- 10
1
vote
1 answer
EF Core one-way self referencing entity binding
I have set up a self referential entity using EF Core which looks like this:
Entity
public class DetailType
{
public int DetailTypeId { get; set; }
public string Name { get; set; }
public int? ParentTypeId { get; set; }
public…

Andy Furniss
- 3,814
- 6
- 31
- 56
1
vote
0 answers
How to do strong_params with a multi-nested self-referential child association?
So, I have a Group object that has child conditions. The Condition object also has a self-referential child association for conditions:
Group
has_many :conditions
Condition
belongs_to :group
has_many :conditions
Is there a nice way to specify…

surjay
- 123
- 1
- 8
1
vote
1 answer
Self-referential foreign key relationship on SQLAlchemy declarative mixin class with declared_attr
I have a mixin class that I define near the beginning of the SQLAlchemy app and then inherit on pretty much every declarative model I use.
class Owned(object):
@declared_attr
def created_by_id(cls):
return Column(Integer,…

snoopy91
- 337
- 3
- 12
1
vote
0 answers
copy rows recursively in self-referencing table in sql
I have a table like this :
CREATE TABLE IF NOT EXISTS THING(
Id int NOT NULL IDENTITY(1, 1),
IdParent int,
randomtext varchar(255)
)
I would like to copy given list of ids with their children, grandchildren,…

Amanite Laurine
- 1,149
- 1
- 11
- 22
1
vote
1 answer
EF Core - Self-referencing Many to Many relationship
Entity model:
public class DocumentType : CodeBase
{
[Required]
[MaxLength(100)]
public string Name { get; set; }
public TimeSpan? Productiontime { get; set; }
public bool IsDeliverable { get; set; }
public virtual…

user2963570
- 381
- 6
- 21
1
vote
2 answers
Django model self-reference: prevent referencing to itself
I have the following model:
class Category(models.Model):
name = models.CharField(max_length=255)
parent = models.ForeignKey('self', related_name='children')
My question is that how I could prevent model to referencing itself (same object).…

m5seppal
- 1,186
- 3
- 15
- 31
1
vote
1 answer
SQLAlchemy: How to query an optional column on a self referential adjacency list table?
I have the following Table model representing a timeline.
class TimeRange(Base):
__tablename__ = "time_line"
record_id = Column(Integer, primary_key=True)
level = Column(String, nullable=False) # e.g. "Point", "Range"
content =…

Jinghui Niu
- 990
- 11
- 28
1
vote
1 answer
Model callbacks not working with self referential association
I am having a model Evaluation that has many sub evaluations (self refential)
class Evaluation < ApplicationRecord
has_many :sub_evaluations, class_name: "Evaluation", foreign_key: "parent_id", dependent: :destroy
before_save…

webster
- 3,902
- 6
- 37
- 59
1
vote
1 answer
Self-referencing table in EF6
I thought this was going to be easy... I have a situation where I have a table Module, which can contain "base" modules, and "compound" modules (that are made up from 1-n base modules).
So I have these two tables in SQL Server 2014:
CREATE TABLE…

marc_s
- 732,580
- 175
- 1,330
- 1,459
1
vote
0 answers
SQL self-referencing table query as top-down JSON
I have a self referencing table named scope (in my user_account schema) that is hierarchal data - some example data (Postgres 9.4):
*****Edit:******
Made an SQL fiddle: http://sqlfiddle.com/#!15/43ff9/2/0 with a much more detailed example. Note the…

Chris Burrus
- 347
- 3
- 11