All Questions
914
questions
97
votes
6
answers
187k
views
Check if an object exists
I need to check if Model.objects.filter(...) turned up anything, but do not need to insert anything. My code so far is:
user_pass = log_in(request.POST) # form class
if user_pass.is_valid():
...
97
votes
1
answer
56k
views
Django's ManyToMany Relationship with Additional Fields
I want to store some additional information in that, automatically created, ManyToMany join-table. How would I do that in Django?
In my case I have two tables: "Employees" and "Projects". What I ...
68
votes
5
answers
88k
views
Populating django field with pre_save()?
class TodoList(models.Model):
title = models.CharField(maxlength=100)
slug = models.SlugField(maxlength=100)
def save(self):
self.slug = title
super(TodoList, self).save()
...
65
votes
3
answers
44k
views
How can I iterate over ManyToManyField?
A simple question and yet I can't find an answer for it.
I have a model with a ManyToMany field:
class Stuff(models.Model):
things = models.ManyToManyField(Thing)
then in a different function I ...
43
votes
6
answers
8k
views
Ruby on Rails: Is it better to validate in the model or the database?
Is it generally better practice (and why) to validate attributes in the model or in the database definition?
For (a trivial) example:
In the user model:
validates_presence_of :name
versus in the ...
40
votes
4
answers
30k
views
How to implement Active Record inheritance in Ruby on Rails?
How to implement inheritance with active records?
For example, I want a class Animal, class Dog, and class Cat.
How would the model and the database table mapping be?
36
votes
7
answers
49k
views
In Laravel, how do I retrieve a random user_id from the Users table for Model Factory seeding data generation?
Currently, in my ModelFactory.php, I have:
$factory->define(App\Reply::class, function (Faker\Generator $faker) {
return [
'thread_id' => 1,
'user_id' => 1,
'body' => $faker-...
24
votes
2
answers
11k
views
Turn off "updated_at" column in Rails
I have a simple "Log" model, that records the fact of calling controller's action.
Entries of this "log" record are supposed to be created once and never altered. Also, I will have many of these ...
20
votes
2
answers
17k
views
Django: Grab a set of objects from ID list (and sort by timestamp)
I have a list of IDs for objects that I need to grab, then I have to sort them by their timestamp. Here's how I was going to do it:
For i in object_ids:
instance = Model.objects.get(id = i)
# ...
20
votes
3
answers
51k
views
Import data from excel spreadsheet to django model
I'm building a website that'll have a django backend. I want to be able to serve the medical billing data from a database that django will have access to. However, all of the data we receive is in ...
18
votes
3
answers
4k
views
When to split up models into multiple database tables? [closed]
I'm working with Ruby on Rails, but this question I think is broader than that and applies to database design generally.
When is it a good idea to split a single model up into multiple tables? For ...
17
votes
4
answers
5k
views
How can I create a case-insensitive database index in Django?
I am using Django to create some database tables, as so:
class MetadataTerms(models.Model):
term = models.CharField(max_length=200)
size = models.IntegerField(default=0)
validity = models....
14
votes
1
answer
16k
views
Laravel what is use HasFactory?
I created a model in Laravel. I always noticed the default would be use HasFactory. May I know what exactly does it work. In my understanding from reading documentation, it is for linking to database (...
12
votes
2
answers
3k
views
Determining when there are new versions in core data model
Short question:
I want to run a certain code in my app only if my Core Data model has changed (new entities, new properties, etc). How can I determine if the model has changed or not?
Just some ...
11
votes
4
answers
12k
views
Treat NULL as '0' in Django model
I use the following bit of code in my Django app:
pictures = gallery.picture_set.annotate( score=models.Sum( 'picturevote__value' ) ).order_by( '-score' )
There is a table of galleries. In each of ...
11
votes
4
answers
16k
views
Properly calling the database from Model in an MVC application?
I'm building a tiny MVC framework for learning/experimenting and small project purposes. I needed to find out the basics of the internals of the Model since a full MVC framework and ORM is overkill ...
11
votes
1
answer
7k
views
How to access model to query database from a helper function?
I am trying to create authentication for my php website. I am using the codeigniter framework. I will have to validate the session in all the pages of my website. So to avoid the code repetition, i am ...
10
votes
3
answers
6k
views
Django DB Models F Combined Expression
In [1]: from django.db.models import F
In [2]: from forum.models import Post
In [3]: post = Post.objects.get(id=1)
In [4]: post.view_count = F('view_count') + 1
In [5]: post.save()
In [6]: post....
10
votes
1
answer
3k
views
Magento catching exceptions and rolling back database transactions
I'm working on a Magento module and need to know if it's possible to roll back a series of model saves. Basically, I have five models plus several from my module that I need to save one after the ...
9
votes
2
answers
16k
views
How do I stop this cascading delete from happening in Django?
I have three model classes in a Django app:
class Folder(models.Model):
...
folder = models.ForeignKey('Folder',null=True,blank=True,related_name='folders')
front_thumbnail_image = models....
9
votes
1
answer
2k
views
Composite Indexes SQLModel
I'm experimenting with SQLModel (https://sqlmodel.tiangolo.com/) and I get to the point that I had to create a composite index between several fields and I can't how to do it using SQLModel library.
...
9
votes
1
answer
7k
views
Troubleshooting "Error: Unable to serialize database:" when performing dumpdata
For some reason today I cannot dump my database using python manage.py dumpdata or from a link that can download the mysql file.
I tried to use python manage.py dumpdata --traceback and here is the ...
9
votes
3
answers
4k
views
Best Models for a Friendship relation (in Django)
What is the best way to model friendships between users for a social networking site?
The possible states are:
no Friendship
FriendRequest from A to B, B needs to confirm (this is asymmetric)
A and ...
8
votes
5
answers
23k
views
Django: DatabaseError column does not exist
I'm having a problem with Django 1.2.4.
Here is a model:
class Foo(models.Model):
# ...
ftw = models.CharField(blank=True)
bar = models.ForeignKey(Bar, blank=True)
Right after flushing ...
8
votes
3
answers
842
views
Creating Models in ASP.NET MVC
I'm just starting a project in ASP.Net MVC with LINQ to Entities and I was wondering if there was a nice, clean way of defining models that creates the appropriate tables in the database for me. I'm ...
7
votes
4
answers
12k
views
Zend Framework, run query without a view?
I am currently building a small admin section for a website using Zend Framework, this is only my second time of using the framework so I am a little unsure on something things. for example are I have ...
7
votes
3
answers
3k
views
Where do I put my database query tests in rails?
I am coming from a Spring/hibernate background. I have noticed that Rails has no dao and service layers. This really speeds up development, but I don't know where to put my tests sometimes.
Right now,...
7
votes
4
answers
22k
views
LARAVEL 5.5 - Insert Data into Database from FORM with Eloquent Model
I have a problem with inserting Data into Database.
All i have done till now is :
Create a Model with Controller and Migration via:
php artisan make:model Cars -mcr
So, now all of my files looks ...
6
votes
2
answers
18k
views
CakePHP database table, missing datasource default
I found this similar question but my problem is different.
I moved my CakePHP 2.2 application to another server. There exists no problem before migration. Most of the things works fine after ...
6
votes
3
answers
5k
views
Rails db:seed error "undefined method `finder_needs_type_condition?' for nil:NilClass"
I have a problem when attempting to populate my sqlite db. There's not much info regarding the specific error "finder_needs_type_condition?" that I can find, but I don't have much experience with ...
6
votes
4
answers
295
views
Prevent upvote model from being called for every comment
I have three models: User, Comment and Upvote. User-to-Comment has a one-to-many relation, Comment-to-Upvote has a one-to-many relation and User-to-Upvote has a one-to-many relation.
I want to do ...
6
votes
3
answers
6k
views
Undefined method File::save() (Laravel model)
I am using Laravel to build a new web project. I am using Eloquent (its ORM) to do all the database related stuff. I have a SQLite database with two tables: 'images' and 'files'. Therefore, I have two ...
6
votes
8
answers
1k
views
in a web application, how do keep the database structure up to date?
If your data change after the application is deployed, how do you keep the database up to date?
I mean, you can add or remove table, thats a simple task. Altering an existing table can be trivial ...
6
votes
3
answers
213
views
Is it a good programming practice to separate models from the rest of the application
My project consists of several django applications that need to be deployed differently, possibly on different machines. However often these apps occasionally need to access each other's models, so I ...
6
votes
4
answers
6k
views
how to create a Django models that doesn't map to a database table
I want to create a model that doesn't map to a database table. Instead, stays in memory as a python object.
Actually, this model is supposed to represents normalised data from many other table-...
6
votes
1
answer
11k
views
how do i see cakephp database save errors?
if i have a cake php saveAll method like so:
if ($this->Video->saveAll($this->data)){
... // stuff that never happens, sadly
} else {
...
$this->Session->setFlash('boo! hss!...
6
votes
3
answers
5k
views
Best database design for a "sensor system"
I'm doing a schoolwork and..
I have to do a vehicle tracker system. I thought of these three designs. What do you think?
My database schemas
Opinions?
6
votes
2
answers
3k
views
Generate models from existing tables using Rails 3
Using Rails 3.2.2 and ruby 1.9.3dev and mysql
I am new to ruby and rails. We have an existing database with a couple hundred tables. We would like to try out rails to see if it would be a positive ...
5
votes
3
answers
16k
views
UPDATE query with CakePHP
I know I can use $this->Model->save() to update a particular record if I pass the id in, but how can I update a single field on that row?
I have a users table with a balance field. I want to ...
5
votes
6
answers
4k
views
Can a model link to a View in the database instead of a table in CakePHP?
I was wondering if it was possible to create a view in a database then be able to link a Model to it?
5
votes
3
answers
29k
views
Sequelize validation throwing error
I'm trying to check that a username is unique, and I gather that I'd need a custom validation for that. I've written the following code, but instead of returning the error in the array returned by ....
5
votes
1
answer
4k
views
Model from existing table in Rails 2
I have a database with tables. I want to create a model in my Rails app from existing table. As i know, such functionality is available, and is done as follows:
script/generate scaffold model_name --...
5
votes
3
answers
1k
views
php DAL - separate entity and database?
I've been researching and reading a lot about working with separate layers in PHP to create maintainable and readable code.
However, I see a lot of code where the entity and the database access is ...
5
votes
1
answer
10k
views
Laravel Model's table name with variable
I want to change the table name depending on which user is Auth.
Why? Because when I add a dealer, I create a database client for this dealer and the name of the data is d.$dealer_id.clients. So, the ...
5
votes
5
answers
2k
views
Ad hoc data and repository pattern
What is the recommended way to return ad hoc (custom case by case) data from repository which don't fit any model entities or which extend some?
The 101 example would be the ubiquitous hello word ...
5
votes
0
answers
302
views
last_login field has removed from django user model
I am using django admin application. I just migrated jango 1.9 to Django 3.1.1. And Using python 3.6
During Django migration it would expects an additional arguments as "on_delete". So, I ...
5
votes
1
answer
1k
views
Change database connection according to domain
We have multiple domain running on the same application.
Currently , our database environment setting is having config database replicated to every database server. The slave server owns config ...
5
votes
2
answers
2k
views
Multiple DAOs with dependencies (foreign key)
I am creating an application with database access. I do not use JPA nor JooQ or other frameworks for reasons (and do not want to. Also this is not important for my question). So I use JDBC and write ...
5
votes
2
answers
2k
views
Magento Checkout Terms and Conditions, where are they stored?
Magento has the possibility to enable Checkout Terms and Conditions and so diplay some text and a checkbox in the checkout page.
The feature is actionable following these steps: http://www....
4
votes
2
answers
5k
views
Unable to move (click and drag) entities in MySQL Workbench EER Diagram
I am modelling a database using MySQL Workbench - EER model. Now, I would like to move around certain entities so I can wrap them in separate layer for better visual look. My problem is that I am not ...