foreign-keys

(Django) (Foreign Key Issues) model.person_id May not be NULL

血红的双手。 提交于 2019-12-05 02:20:18
问题 I know this seems to be an over-asked question in the Django circles but I'm afraid to say that i've not yet found the solution to this. My Model - from djago.... import User class InfoPersonal(models.Model): ... person = models.OneToOneField(User) I've tried overriding the save_model() in the admin definition and also overriding the save() in the Form but nothing seems to work. If you were to auto add data into a ForeignKey or OneToOneField column to a Model how would you do it? def profile

Primary key and foreign key at the same time with doctrine 2

点点圈 提交于 2019-12-05 02:17:50
I have the two tables : table A with id as primary key table B with id as primary key and foreign key Explanation on short: I need to have in table B a primary key that also to be a foreign key that points to table A 's primary key. Can anybody explain me how to map this by annotations in Doctrine 2? Note: I tried it By this : class A { /** * @var bigint $id * * @Column(name="id", type="bigint", nullable=false) * @Id * @GeneratedValue(strategy="IDENTITY") */ private $a_id; ... and B table: class B { /** * @var bigint $id * @Id * @OneToOne(targetEntity="A", fetch="LAZY") * @JoinColumn(name="id"

SQLAlchemy ForeignKey can't find table

て烟熏妆下的殇ゞ 提交于 2019-12-05 02:06:26
Getting this error when I try to instantiate the ConsumerAdvice class. Foreign key associated with column 'tbConsumerAdvice.ConsumerAdviceCategory_ID' could not find table 'tbConsumerAdviceCategories' with which to generate a foreign key to target column 'ID_ConsumerAdviceCategories' class ConsumerAdviceCategory(Base): __tablename__ = 'tbConsumerAdviceCategories' __table_args__ = {'schema':'dbo'} ID_ConsumerAdviceCategories = Column(INTEGER, Sequence('idcac'),\ primary_key=True) Name = Column(VARCHAR(50), nullable=False) def __init__(self,Name): self.Name = Name def __repr__(self): return "<

EntityFramework 6 AddOrUpdate not working with compound or composite primary key

久未见 提交于 2019-12-05 01:56:50
The issue has been my weekend nightmare... I have a table where AddOrUpdate is not working correctly, it keeps adding but never updating. All I want to do is when I add a new entity to the table using AddOrUpdate I want it to check the AppointmentId and CompletionCodeId columns and if they match than update, otherwise add. Table structure: CREATE TABLE [dbo].[AppointmentCodes] ( [Id] INT IDENTITY (1, 1) NOT NULL, [AppointmentId] INT NOT NULL, [Quantity] INT NOT NULL, [CompletionCodeId] INT NOT NULL, CONSTRAINT [PK_AppointmentCodes] PRIMARY KEY CLUSTERED ([Id] ASC, [AppointmentId] ASC)); ^^ Not

How to find if a referenced object can be deleted?

时间秒杀一切 提交于 2019-12-05 01:48:42
I have an object called "Customer" which will be used in the other tables as foreign keys. The problem is that I want to know if a "Customer" can be deleted (ie, it is not being referenced in any other tables). Is this possible with Nhibernate? Jaguar What you are asking is to find the existence of the Customer PK value in the referenced tables FK column. There are many ways you can go about this: as kgiannakakis noted, try to do the delete and if an exception is thrown rollback. Effective but ugly and not useful. This also requires that you have set a CASCADE="RESTRICT" in your database. This

EF One-to-many Foreign Keys without child navigation properties

情到浓时终转凉″ 提交于 2019-12-05 01:08:27
Using code-first Entity Framework and .NET 4, I'm trying to create a one-to-many relationship between parents to children: public class Parent { [Key] public int ParentId { get; set; } [Required] public string ParentName { get; set; } public IEnumerable<Child> Children { get; set; } } public class Child { [Key] public int ChildId { get; set; } [ForeignKey] public int ParentId { get; set; } [Required] public string ChildName { get; set; } } As pointed out here , in order for foreign key relationship to carry into the database, the actual objects must be linked, not just their IDs. The normal

django: How to make one form from multiple models containing foreignkeys

核能气质少年 提交于 2019-12-05 00:51:39
问题 I am trying to make a form on one page that uses multiple models. The models reference each other. I am having trouble getting the form to validate because I cant figure out how to get the id of two of the models used in the form into the form to validate it. I used a hidden key in the template but I cant figure out how to make it work in the views My code is below: views: def the_view(request, a_id,): if request.method == 'POST': b_form= BForm(request.POST) c_form =CForm(request.POST) print

“polymorphism” for FOREIGN KEY constraints

左心房为你撑大大i 提交于 2019-12-05 00:27:56
There is this field in a table: room_id INT NOT NULL CONSTRAINT room_id_ref_room REFERENCES room I have three 2 tables for two kinds of rooms: standard_room and family_room How to do something like this: room_id INT NOT NULL CONSTRAINT room_id_ref_room REFERENCES standard_room or family_room I mean, room_id should reference either standard_room or family_room . Is it possible to do so? Here is the pattern I've been using. CREATE TABLE room ( room_id serial primary key, room_type VARCHAR not null, CHECK CONSTRAINT room_type in ("standard_room","family_room"), UNIQUE (room_id, room_type) );

How to find out whether a model's column is a foreign key?

六月ゝ 毕业季﹏ 提交于 2019-12-05 00:27:12
I'm dynamically storing information in the database depending on the request: // table, id and column are provided by the request table_obj = getattr(models, table) record = table_obj.objects.get(pk=id) setattr(record, column, request.POST['value']) The problem is that request.POST['value'] sometimes contains a foreign record's primary key (i.e. an integer) whereas Django expects the column's value to be an object of type ForeignModel: Cannot assign "u'122'": "ModelA.b" must be a "ModelB" instance. Now, is there an elegant way to dynamically check whether b is a column containing foreign keys

Entity Framework 6: detect relationship changes

∥☆過路亽.° 提交于 2019-12-05 00:18:04
问题 in my DbContext subclass I have overridden the SaveChanges() method so I can implement a sort of Trigger-like functionality (before the changes are actually saved). Now, in some of these triggers it is necessary to detect whether certain relationships have changed, regardless of many-to-many, one-to-one/zero etc. I have read a number of posts on the internet, including some on this site, that mention that the DbContext API doesn't expose any means of getting relationship info. However,