foreign-keys

How to count items in a foreign key?

微笑、不失礼 提交于 2019-12-24 10:31:02
问题 I am making a blog app using django... This is my model.py class categories(models.Model): Title = models.CharField(max_length=40) class Blog(models.Model): User = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True) Date = models.DateTimeField(default=datetime.now) Blog_title = models.CharField(max_length=255) likes = models.ManyToManyField(settings.AUTH_USER_MODEL,related_name='likes',blank=True) Description = RichTextUploadingField(blank=True, null=True

Query self-join with Sequelize, including related record

十年热恋 提交于 2019-12-24 08:35:08
问题 We're using Postgres for a Node.js app and have a Sequelize model Entry which is roughly defined as: const entriesModel = sequelize.define('Entry', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, post_date: { type: DataTypes.DATE, allowNull: false, defaultValue: () => new Date() } /* ...more fields here, etc, etc... */ }, { classMethods: { associate: (models) => { entriesModel.hasOne(models.Entry, { onDelete: 'CASCADE', foreignKey: { name: 'parent_id', allowNull:

cannot drop a foreign key in mySQL

白昼怎懂夜的黑 提交于 2019-12-24 08:26:02
问题 It's a common example between Persons and Orders. I just copied it from Internet as a test. CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, PRIMARY KEY (ID) ); CREATE TABLE Orders ( OrderID int NOT NULL, OrderNumber int NOT NULL, PersonID int, PRIMARY KEY (OrderID), FOREIGN KEY (PersonID) REFERENCES Persons(ID) ); Till now it's all successful. But how can I drop the Foreign Key PersonID? I tried this. ALTER TABLE Orders DROP FOREIGN KEY

SubSonic isn't generating MySql foreign key tables

你说的曾经没有我的故事 提交于 2019-12-24 08:24:00
问题 I two tables within a MySql 5.1.34 database. When using SubSonic to generate the DAL, the foreign-key relationship doesn't get scripted, ie; I have no Parent.ChildCollection object. Looking inside the generated DAL Parent class shows the following; //no foreign key tables defined (0) I have tried SubSonic 2.1 and 2.2, and various MySql 5 versions. I must be doing something wrong procedurally - any help would be greatly appreciated. This has always just worked 'out-the-box' when using MS-SQL.

how to show foreign key value instead of its ID

喜欢而已 提交于 2019-12-24 08:04:51
问题 i'm try to display foreign key value , but it instead return the id , in template models.py class Product(models.Model): product_name = models.CharField(unique=True, max_length=50) pass def __str__(self): return self.product_name class Order(models.Model): id = models.AutoField(primary_key = True) products = models.ManyToManyField(Product ,through='ProductOrder') pass def __str__(self): return str(self.products.all()) class ProductOrder(models.Model): product = models.ForeignKey(Product, on

Sequelize belongsToMany validation error

走远了吗. 提交于 2019-12-24 07:38:18
问题 I'm facing a problem since 3 days. I would like to create a multiple relation n:m in Sequelize but I'm getting an error message : " Unhandled rejection SequelizeUniqueConstraintError: Validation error ". So I have 3 tables : Serie. Episode. Season (in this table I would like to add relation to Serie and Episode ). Here is the definition of Serie : var Serie = sequelize.define('Serie', { idSerie: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, name: DataTypes.STRING }, {

whereHas on multiple relationships - laravel 5.4

守給你的承諾、 提交于 2019-12-24 07:24:23
问题 I have multiple tables. Tour Table id | title | slug ..... Image Table id | tour_id | image..... Inclusion Table id | tour_id | inclusion.... Exclusion Table id | tour_id | exclusion... Itenary Table id | tour_id | day_summary..... Tour_User Table (This is a pivot table between tour table and user table) tour_id | user_id I Need I want to get tour details with image, inclusion, exclusion, itenary, tour user where image.tour_id equals tour.id and inclusion.tour_id equals tour.id and exclusion

Saving formset with drop-down-menu Foreignkey: IntegrityError XXX_id may not be NULL

旧城冷巷雨未停 提交于 2019-12-24 03:35:13
问题 I am trying to have a formset where each form (PropertySelector) has a drop-down menu (PropertySelector.property) whereas each item of that menu is ForeignKey reference to another model (Property). Somehow when I am trying to submit and save the formset I am getting: Exception Type: IntegrityError Exception Value: testproj_propertyselector.property_id may not be NULL What is wrong with it and how can I get around it? My entire code is below. Thanks. EDIT: it looks like inline_formset problem

Conditional CASCADE operation for foreign key constraint?

好久不见. 提交于 2019-12-24 01:54:43
问题 I have parent and child table where child has a FK pointing to the PK of parent table. When I delete something in parent table I can have child records deleted as well by having ON DELETE CASCADE . However, in my parent table I don't delete records at all. Instead I set the column state = "passive" . I want to delete related entries in the child table. Do we have something like a "conditional CASCADE" in Postgres? Or is the solution to manually delete entries in the child table? 回答1: You

SQLiteConstraintException: How to map “no relationship” with a FOREIGN KEY in Room

你。 提交于 2019-12-24 01:13:06
问题 I am using the Android Room Persistence Library as ORM. I have the following Entity: @Entity(tableName = "log_entries", foreignKeys = { @ForeignKey( entity = Serving.class, parentColumns = "id", childColumns = "foodId", onDelete = ForeignKey.CASCADE ) } ) public class LogEntry { @PrimaryKey(autoGenerate = true) private long id; private long servingId; // ... } There are some log entries which have a serving and some which don't. Adding a log entry which has a serving works fine, but adding