database-relations

Delete all rows which has no id existing in another table

帅比萌擦擦* 提交于 2019-11-29 11:05:25
I want to delete all rows which has no existing foreign key in another table example: table1 +----+-------+ |id | data | +----+-------+ | 1 | hi | +----+-------+ | 2 | hi | +----+-------+ | 3 | hi | +----+-------+ | 4 | hi | +----+-------+ | 5 | hi | +----+-------+ table2 +----+-------+ |a_id| data | +----+-------+ | 1 | hi | +----+-------+ | 20 | hi | +----+-------+ | 3 | hi | +----+-------+ | 40 | hi | +----+-------+ | 5 | hi | +----+-------+ The query will delete rows with id# 20 and 40 on table2. I need to do this so that i could establish a relationship with table1 and table2. eggyal

Cascade delete in Ruby ActiveRecord models?

瘦欲@ 提交于 2019-11-28 22:18:15
I was following the screencast on rubyonrails.org (creating the blog). I have following models: comment.rb class Comment < ActiveRecord::Base belongs_to :post validates_presence_of :body # I added this end post.rb class Post < ActiveRecord::Base validates_presence_of :body, :title has_many :comments end Relations between models work fine, except for one thing - when I delete a post record, I'd expect RoR to delete all related comment records. I understand that ActiveRecords is database independent, so there's no built-in way to create foreign key, relations, ON DELETE, ON UPDATE statements. So

How do you track record relations in NoSQL?

孤街醉人 提交于 2019-11-27 16:46:59
I am trying to figure out the equivalent of foreign keys and indexes in NoSQL KVP or Document databases. Since there are no pivotal tables (to add keys marking a relation between two objects) I am really stumped as to how you would be able to retrieve data in a way that would be useful for normal web pages. Say I have a user, and this user leaves many comments all over the site. The only way I can think of to keep track of that users comments is to Embed them in the user object (which seems quite useless) Create and maintain a user_id:comments value that contains a list of each comment's key

Cascade delete in Ruby ActiveRecord models?

核能气质少年 提交于 2019-11-27 13:24:43
问题 I was following the screencast on rubyonrails.org (creating the blog). I have following models: comment.rb class Comment < ActiveRecord::Base belongs_to :post validates_presence_of :body # I added this end post.rb class Post < ActiveRecord::Base validates_presence_of :body, :title has_many :comments end Relations between models work fine, except for one thing - when I delete a post record, I'd expect RoR to delete all related comment records. I understand that ActiveRecords is database

Relations on composite keys using sqlalchemy

允我心安 提交于 2019-11-27 10:56:26
I have this simple model of Author - Books and can't find a way to make firstName and lastName a composite key and use it in relation. Any ideas? from sqlalchemy import create_engine, ForeignKey, Column, String, Integer from sqlalchemy.orm import relationship, sessionmaker from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() engine = create_engine('mssql://user:pass@library') engine.echo = True session = sessionmaker(engine)() class Author(Base): __tablename__ = 'authors' firstName = Column(String(20), primary_key=True) lastName = Column(String(20), primary_key

How do you track record relations in NoSQL?

橙三吉。 提交于 2019-11-26 18:45:19
问题 I am trying to figure out the equivalent of foreign keys and indexes in NoSQL KVP or Document databases. Since there are no pivotal tables (to add keys marking a relation between two objects) I am really stumped as to how you would be able to retrieve data in a way that would be useful for normal web pages. Say I have a user, and this user leaves many comments all over the site. The only way I can think of to keep track of that users comments is to Embed them in the user object (which seems

Using build with a has_one association in rails

☆樱花仙子☆ 提交于 2019-11-26 16:53:44
In this example, I create a user with no profile , then later on create a profile for that user. I tried using build with a has_one association but that blew up. The only way I see this working is using has_many . The user is supposed to only have at most one profile . I have been trying this. I have: class User < ActiveRecord::Base has_one :profile end class Profile < ActiveRecord::Base belongs_to :user end But when I do: user.build_profile I get the error: ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'profiles.user_id' in 'where clause': SELECT * FROM `profiles` WHERE (

Relations on composite keys using sqlalchemy

核能气质少年 提交于 2019-11-26 10:21:52
问题 I have this simple model of Author - Books and can\'t find a way to make firstName and lastName a composite key and use it in relation. Any ideas? from sqlalchemy import create_engine, ForeignKey, Column, String, Integer from sqlalchemy.orm import relationship, sessionmaker from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() engine = create_engine(\'mssql://user:pass@library\') engine.echo = True session = sessionmaker(engine)() class Author(Base): __tablename__