foreign-keys

Can't create super user with custom user model in Django 1.5

可紊 提交于 2019-12-04 11:52:51
问题 my goal is to create a custom user model in Django 1.5 # myapp.models.py from django.contrib.auth.models import AbstractBaseUser class MyUser(AbstractBaseUser): email = models.EmailField( verbose_name='email address', max_length=255, unique=True, db_index=True, ) first_name = models.CharField(max_length=30, blank=True) last_name = models.CharField(max_length=30, blank=True) company = models.ForeignKey('Company') ... USERNAME_FIELD = 'email' REQUIRED_FIELDS = ['company'] I can't create a super

Circular dependencies in foreign keys: use it or avoid it?

只谈情不闲聊 提交于 2019-12-04 11:48:34
问题 My application loads lots of data from a database into a complex data structure. The in-memory data structure ressembles the structure of the database, which means that if the database contains the following tables: table A, key is A1 table B, key is B1, one of the columns is a foreign key to [the key of] table A table C, key is C1, one of the columns is a foreign key to [the key of] table B Then I have classes A, B and C, and: a data member of B (B::m_a) is a pointer to A a data member of C

Django Foreign Key: get related model?

被刻印的时光 ゝ 提交于 2019-12-04 10:16:49
问题 Is it possible to get the related model of a foreign key through the foreign key field itself? For example, if I have 3 models: class ModelA(models.Model) field1 = models.CharField(max_length=10) class ModelB(models.Model) field1 = models.CharField(max_length=10) class ModelC(models.Model) field1 = models.CharField(max_length=10) field2 = models.ForeignKey(ModelA) field3 = models.ForeignKey(ModelB) and I want to do: for field in ModelC._meta.fields: if field.get_internal_type() == "ForeignKey

SQL Server 2005: Nullable Foreign Key Constraint

◇◆丶佛笑我妖孽 提交于 2019-12-04 10:16:39
问题 I have a foreign key constraint between tables Sessions and Users. Specifically, Sessions.UID = Users.ID. Sometimes, I want Sessions.UID to be null. Can this be allowed? Any time I try to do this, I get an FK Constraint Violation. Specifically, I'm inserting a row into Sessions via LINQ. I set the Session.User = null; and I get this error: An attempt was made to remove a relationship between a User and a Session. However, one of the relationship's foreign keys (Session.UID) cannot be set to

How to set up custom string foreign key in Rails 4?

核能气质少年 提交于 2019-12-04 10:09:32
How to set up associations to set up properly has_one using string foreign_key? class Pharmaceutic < ActiveRecord::Base has_one :pharmaceutic_group, foreign_key: "code" end class PharmaceuticGroup < ActiveRecord::Base belongs_to :pharmaceutic, primary_key: 'code' end >> Pharmaceutic.last.pharmaceutic_group Pharmaceutic Load (0.3ms) SELECT `pharmaceutics`.* FROM `pharmaceutics` ORDER BY `pharmaceutics`.`id` DESC LIMIT 1 PharmaceuticGroup Load (0.3ms) SELECT `pharmaceutic_groups`.* FROM `pharmaceutic_groups` WHERE `pharmaceutic_groups`.`code` = 2 ORDER BY `pharmaceutic_groups`.`id` ASC LIMIT 1 =

Django MTMField: limit_choices_to = other_ForeignKeyField_on_same_model?

喜夏-厌秋 提交于 2019-12-04 08:53:14
I've got a couple django models that look like this: from django.contrib.sites.models import Site class Photo(models.Model): title = models.CharField(max_length=100) site = models.ForeignKey(Site) file = models.ImageField(upload_to=get_site_profile_path) def __unicode__(self): return self.title class Gallery(models.Model): name = models.CharField(max_length=40) site = models.ForeignKey(Site) photos = models.ManyToManyField(Photo, limit_choices_to = {'site':name} ) def __unicode__(self): return self.name I'm having all kinds of fun trying to get the limit_choices_to working on the Gallery model

postgresql foreign key syntax

眉间皱痕 提交于 2019-12-04 07:23:00
问题 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

Foreign key constraints while dumping data

。_饼干妹妹 提交于 2019-12-04 06:59:43
问题 mysqldump --compact --no-create-info -h192.168.150.180 -uroot -p live pnlbus_stops | sed s/pnlbus_stops/bus_stops/g | mysql test I am getting an error: ERROR 1062 (23000) at line 1: Duplicate entry 'AN' for key 1 This is because bus_stops table in the test DB has foreign key constraints. How do I truncate the bus_stops table from test database in a SINGLE STATEMENT before inserting from "live" DB? 回答1: put set FOREIGN_KEY_CHECKS = 0; at the top of your dump file and put SET FOREIGN_KEY_CHECKS

Room composite Primary Key link to Foreign Key

被刻印的时光 ゝ 提交于 2019-12-04 05:51:08
I am implementing an android application with Room database and have a small question about relationships in this DB. I have two tables: @Entity(tableName = "foods", primaryKeys = {"id", "language_id"}, indices = {@Index(value = {"id", "language_id"}, unique = true)}, inheritSuperIndices = true) public class Food { @NonNull @ColumnInfo(name = "id") private String mId; @NonNull @ColumnInfo(name = "language_id") private String mLanguageId; } @Entity(tableName = "ingredients", primaryKeys = {"id", "language_id"}, indices = {@Index(value = {"id", "language_id"}, unique = true), @Index(value = {

Primary and foreign keys in xml schema

 ̄綄美尐妖づ 提交于 2019-12-04 05:35:53
I am trying to insert primary and foreign keys in XSD schema file below :- Primary key is StudentID and Foreign Keys are courseID, AddressID, GradeID. <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Student"> <xs:complexType> <xs:sequence> <xs:element name="Title" type="xs:string"/> <xs:element name="FirstName" type="xs:string"/> <xs:element name="LastName" type="xs:string"/> <xs:element name="Dateborn" type="xs:date"/> <xs:element name="Gender" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> However above code is