foreign-key-relationship

Django: list all reverse relations of a model

好久不见. 提交于 2019-11-29 01:32:16
I would like my django application to serve a list of any model's fields (this will help the GUI build itself). Imagine the classes (ignore the fact that all field of Steps could be in Item , I have my reasons :-) ) class Item(models.Model): name = models.CharField(max_length=100) description = models.TextField() class Steps(models.Model): item = models.OneToOneField('Item', related_name='steps') design = models.BooleanField(default=False) prototype = models.BooleanField(default=False) production = models.BooleanField(default=False) Now, when I want to list a model's fields: def get_fields

SQL Server 2008: The columns in table do not match an existing primary key or unique constraint

你离开我真会死。 提交于 2019-11-29 01:22:31
问题 I need to make some changes to a SQL Server 2008 database. This requires the creation of a new table, and inserting a foreign key in the new table that references the Primary key of an already existing table. So I want to set up a relationship between my new tblTwo, which references the primary key of tblOne. However when I tried to do this (through SQL Server Management Studio) I got the following error: The columns in table 'tblOne' do not match an existing primary key or UNIQUE constraint

Rails: Non id foreign key lookup ActiveRecord

对着背影说爱祢 提交于 2019-11-29 00:39:43
问题 I want ActiveRecord to lookup by a non-id column from a table. Hope this is clear when I give you my code sample. class CoachClass < ActiveRecord::Base belongs_to :coach end class Coach < ActiveRecord::Base has_many :coach_classes, :foreign_key => 'user_name' end When I do a coach_obj.coach_classes , this rightly triggers SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 2) (2 being the that coach's id here which is my problem.) I want it to trigger SELECT * FROM `coach_classes

unable to drop the foreign key

半腔热情 提交于 2019-11-28 22:50:34
I would like to drop the foreign key in my table but been into this error message mysql> alter table customers drop foreign key customerid; ERROR 1025 (HY000): Error on rename of '.\products\customers' to '.\products\#sql2-7ec-a3' (errno: 152) mysql> To avoid getting this error while trying to drop a foreign key, use the constraint name rather than the column name of the foreign key. When I tried mysql> ALTER TABLE mytable DROP PRIMARY KEY; I got error as ERROR 1025 (HY000): Error on rename of '.\database\#sql-454_3' to '.\database\mytable' (errno: 150). I solved it using: mysql> ALTER TABLE

Zend Framework 2 - Hydrator strategy for Doctrine relationship not working

做~自己de王妃 提交于 2019-11-28 19:02:19
As mentioned here I'm building a custom hydration strategy to handle my related objects in a select box in a form. My form looks like this: $builder = new AnnotationBuilder($entityManager); $form = $builder->createForm(new MyEntity()); $form->add(new MyFieldSet()); $hydrator = new ClassMethodsHydrator(); $hydrator->addStrategy('my_attribute', new MyHydrationStrategy()); $form->setHydrator($hydrator); $form->get('my_attribute')->setValueOptions( $entityManager->getRepository('SecEntity\Entity\SecEntity')->fetchAllAsArray() ); When I add a new MyEntity via the addAction everything works great. I

How to disable Constraints for all the tables and enable it?

被刻印的时光 ゝ 提交于 2019-11-28 17:52:14
I have 60 tables. I want to drop 10 tables where these 10 tables are Constraints(PK,FK) to other 20 tables. While dropping these 10 tables, I need to truncate or delete data from the other 20 tables. Finally I want to disable all 60 table Constraints(FK,PK) and then enable all 60 table constraints after I am done with my work of adding/dropping tables. Is this possible? When I drop a table it is asking for FK. When I truncate those FK dependencies it also is still showing the same. I don't want to mess with all those FK,PK. I want to know smarter method. Stefan Steiger EXEC sp_MSforeachtable

SQL Add foreign key to existing column

只谈情不闲聊 提交于 2019-11-28 15:17:09
问题 If I am using the following SQL command in SQL Server 2008 to update a table with a foreign key constraint: ALTER TABLE Employees ADD FOREIGN KEY (UserID) REFERENCES ActiveDirectories(id) UserID being my FK column in the Employees table. I'm trying to reference the UserID in my ActiveDirectories table. I receive this error: Foreign key 'UserID' references invalid column 'UserID' in referencing table 'Employees'. 回答1: Error indicates that there is no UserID column in your Employees table. Try

postgresql foreign key syntax

蓝咒 提交于 2019-11-28 15:13:04
I have 2 tables as you will see in my posgresql code below. The first table students has 2 columns, one for student_name and the other student_id which is the primary key. In my second table called tests, this has 4 columns, one for subject_id, one for the subject_name, then one for a student with the higest score in a subject which is highestStudent_id. am trying to make highestStudent_id refer to student_id in my students table. This is the code i have below , am not sure if the syntax is correct: CREATE TABLE students ( student_id SERIAL PRIMARY KEY, player_name TEXT); CREATE TABLE tests (

How to correctly add Foreign Key constraints to SQLite DB using SQLAlchemy [duplicate]

点点圈 提交于 2019-11-28 12:19:38
This question already has an answer here: Sqlite / SQLAlchemy: how to enforce Foreign Keys? 7 answers I'm very new to SQLAlchemy and I'm trying to figure it out. Please have in mind the following test setup: class Nine(Base): __tablename__ = 'nine' __table_args__ = (sqlalchemy.sql.schema.UniqueConstraint('nine_b', name='uq_nine_b'), ) nine_a = sqlalchemy.Column(sqlalchemy.dialects.sqlite.INTEGER(), primary_key=True, autoincrement=False, nullable=False) nine_b = sqlalchemy.Column(sqlalchemy.String(20), nullable=False) class Seven(Base): __tablename__ = 'seven' __table_args__ = (sqlalchemy.sql

Link in django admin to foreign key object

泪湿孤枕 提交于 2019-11-28 08:59:48
I have a model A with a ForeignKey to a model B. In Django admin, how can I add a link in the admin page of model A next to the ForeignKey field which open the admin page of the model B ? You can do the following: models.py (example): model B(models.Model): name = models.CharField(max_length=20) model A(models.Model): field1 = models.CharField(max_length=20) Bkey = models.ForeignKey(B) admin.py from django.core import urlresolvers class AAdmin(admin.ModelAdmin): list_display = ["field1","link_to_B"] def link_to_B(self, obj): link=urlresolvers.reverse("admin:yourapp_b_change", args=[obj.B.id])