foreign-keys

django-admin formfield_for_* change default value per/depending on instance

霸气de小男生 提交于 2019-12-03 21:54:19
I'm trying to change the default value of a foreignkey-formfield to set a Value of an other model depending on the logged in user . But I'm racking my brain on it... This: Changing ForeignKey’s defaults in admin site would an option to change the empty_label, but I need the default_value. #Now I tried the following without errors but it didn't had the desired effect: class EmployeeAdmin(admin.ModelAdmin): ... def formfield_for_foreignkey(self, db_field, request=None, **kwargs): formfields= super(EmployeeAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) if request.user.is

Rails model with foreign_key and link table

北战南征 提交于 2019-12-03 21:42:13
I am trying to create a model for a ruby on rails project that builds relationships between different words. Think of it as a dictionary where the "Links" between two words shows that they can be used synonymously. My DB looks something like this: Words ---- id Links ----- id word1_id word2_id How do I create a relationship between two words, using the link-table. I've tried to create the model but was not sure how to get the link-table into play: class Word < ActiveRecord::Base has_many :synonyms, :class_name => 'Word', :foreign_key => 'word1_id' end In general, if your association has

How to give foreign key a name in RoR 3?

点点圈 提交于 2019-12-03 21:33:18
How can I give foreign key a name in RoR? I use following command to give foreign key: rails generate scaffold Table2 id:integer Table1:references This command adds foreign key of Table1 in Table2 but with default name that is Table1_id . So how can I give custom name to it for example my_table_f_key instead of Table1_id . I'm using Ruby 1.9.2 and Rails 3.0.3. Edit:- In my project.rb model: belongs_to :own, :class_name => User In my user.rb model: has_many :owned_projects, :class_name => Project, :foreign_key => :owner how I created my project model rails generate scaffold Project name:string

Hibernate: Foreign key has the wrong number of columns

给你一囗甜甜゛ 提交于 2019-12-03 20:52:16
问题 I have defined a many-to-many relationship between my two entity classes User and Permission. User has a primary key composite of username and countyId, and my Permission table has a regular integer Id. The table UserPermission has the three foreign keys as its primary key: username, countyId and permissionId. Since this is a legacy database, I won't have the opportunity to do the Right Thing(™) and make an integer primary key on User. I've defined the many-to-many relationship like this in

Null foreign key, in ManyToOne relation using hibernate [4.1.1] annotations

痴心易碎 提交于 2019-12-03 20:12:16
I am trying to persist a one-to-many and a many-to-one relation using Hibernate 4.1.1 but the foreign key is always NULL . There are two entities: Account and Client . A Client could have multiple Accounts while an Account has exactly one Client . Here are the classes (only what matters): Account.java @Entity @Table(name = "account") public class Account implements Serializable { private Client client; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") public long getId() { return id; } @ManyToOne @JoinColumn(name = "id_client") public Client getClient() { return client;

PostgreSQL check constraint for foreign key condition

六眼飞鱼酱① 提交于 2019-12-03 19:07:14
问题 I have a table of users eg: create table "user" ( id serial primary key, name text not null, superuser boolean not null default false ); and a table with jobs: create table job ( id serial primary key, description text ); the jobs can be assigned to users, but only for superusers. other users cannot have jobs assigned. So I have a table whereby I see which job was assigned to which user: create table user_has_job ( user_id integer references "user"(id), job_id integer references job(id),

Use of non-clustered index on guid type column in SQL Server

▼魔方 西西 提交于 2019-12-03 19:05:20
问题 I would like optimize the performance of a database that my team is using for an application. I have been looking for areas to add foreign keys, and in turn index those columns to improve the performance of joins. However, many of our tables are joined on an id that is a GUID type, generated upon insertion of an item, and the data associated with that item in other tables is generally has column item_id containing the GUID. I have read that adding clustered indexes to GUID type columns is a

Polymorphic association foreign key constraints. Is this a good solution?

送分小仙女□ 提交于 2019-12-03 16:32:19
问题 We're using polymorphic associations in our application. We've run into the classic problem: we encountered an invalid foreign key reference, and we can't create a foreign key constraint, because its a polymorphic association. That said, I've done a lot of research on this. I know the downsides of using polymorphic associations, and the upsides. But I found what seems to be a decent solution: http://blog.metaminded.com/2010/11/25/stable-polymorphic-foreign-key-relations-in-rails-with

How to add a Foreign key in Customer table (CreatedBy column) for AspNetUser table (Id column) in ASP.NET MVC 5 Identity 2.0 using Code First

让人想犯罪 __ 提交于 2019-12-03 16:24:09
I have created Empty MVC (ASP.NET Web Application) project using Visual Studio 2013 Update 2 RC & then added AspNet Identity Samples using: PM>Install-Package Microsoft.AspNet.Identity.Samples -Pre I have enabled and added migrations & then updated database, which created default tables. I want to create Customer table which contains 2 columns as Foreign Keys to: Groups table (GroupId column) AspNetUsers table (Id column) So I have created 2 classes Customer & Group and added Foreign Keys using Data-annotations as shown below: namespace IdentitySample.Models { // You can add profile data for

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

情到浓时终转凉″ 提交于 2019-12-03 16:13:48
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 "post" if b_form.is_valid() and c_form.is_valid(): print "valid" b_form.save() c_form.save() return