django.db.utils.IntegrityError: column venue_city contains null values
Looks like you added null=True
after created migration file. Because venue_city
is not a nullable field in your migration file
Follow these steps.
1) Drop venue_city & venue_country from your local table
3) Delete all the migration files you created for these `CharField to a ForeignKey` change
4) execute `python manage.py makemigrations`
5) execute 'python manage.py migrate'
It should work
django.db.utils.IntegrityError: null value in column old_column violates not-null constraint
If you use the option "reuse-db", then pytest keeps the test database.
If you use postgres, then you can list the available databases like this:
psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
---------------+----------+----------+---------+---------+-----------------------
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
test_mydb | vagrant | UTF8 | C.UTF-8 | C.UTF-8 |
mydb | vagrant | UTF8 | C.UTF-8 | C.UTF-8 |
(5 rows)
If you connect to "mydb" (via psql
or manage.py dbshell
), then this is a different DB then your tests use.
Use --create-db to force the re-creation of the DB.
django.db.utils.IntegrityError: NOT NULL constraint failed: app.area_id
In the model below you are trying to add area as a ForeignKey. Since this Prefecture
table has some data already Django does not know what to add in area field for existing rows.
class Prefecture(models.Model):
name = models.CharField(max_length=20, verbose_name='city')
area = models.ForeignKey('Area') # non null-able field Django does
# not know what to add
Simple solution. Provide a default or add null
class Prefecture(models.Model):
name = models.CharField(max_length=20, verbose_name='city')
area = models.ForeignKey('Area', null=True)
Note: for all existing rows in Prefecture
now area
field will be null. You can add this field now for new rows.
django.db.utils.IntegrityError: NOT NULL constraint failed: products_product.image ERROR WITH IMAGE FIELD
need just delete your base and make migrations your app
django.db.utils.IntegrityError: NOT NULL constraint failed: app_user.zip
Your zip
field can't be null and you are not setting any value for it when creating a new user/superuser. Add null=True
, a default value or set it when creating a new user.
Also, zip is a python built-in function, you might want to rename the field to zip_code
or something like that.
Regarding not being able to login to the admin panel with an already created account is probably because you are setting staff
instead of the is_staff
flag required to access the admin. I recommend using the default naming.
Check the full example in the documentation.
Why does Django raised IntegrityError :null value in column user_id violates not-null constraint when a form is committed?
Next to the fix by @solarissmoke (changing instance.owner
to instance.user
) which fixes IntegrityError, you are triggering user_logged_in()
signal in the is_valid()
method here:
def form_valid(self, form):
if form.is_valid():
roote = Post.objects.filter(owner =self.request.user)
instance = form.save(commit=False)
instance.user = self.request.user
# Here you trigger signal that creates UserSession!
user_logged_in.send(self.request.user, request=self.request)
return super(NewLocationCreateForm, self).form_valid(form)
One object is therefore created with the CreateView and another one with this signal. Remove this line in form_valid()
.
EDIT: To include data from signal, simply add this to form_valid()
like this:
def form_valid(self, form):
if form.is_valid():
roote = Post.objects.filter(owner =self.request.user)
instance = form.save(commit=False)
instance.user = self.request.user
instance.ip_address = get_client_ip(self.request)
instance.city_data = get_client_city_data(instance.ip_address)
instance.session_key = self.request.session.session_key
return super(NewLocationCreateForm, self).form_valid(form)
Related Topics
How to Speed Up This Indexed View
How to Remove Duplicates from Table Using SQL Query
How to Migrate Datetime Values to Datetimeoffset in SQL Server
SQL Query to Count() Multiple Tables
Using Pivot to Flip Data from Wide to Tall
Using Variables for Asc and Desc in Order By
Delphi: How to Pass a List as a Parameter to a SQL Query
Does Size of a Varchar Column Matter When Used in Queries
Sql: Select Top 3 Records + Sum of Quantity
How to Change the Primary Key to Be Non-Clustered
Best Practices for Inserting/Updating Large Amount of Data in SQL Server 2008
Find Start and Stop Date for Contiguous Dates in Multiple Rows
SQL Server - Does Trigger Affects @@Rowcount
Postgres: What Is the Query 'Select * from User' Actually Doing
Do You Prefer Verbose Naming When It Comes to Database Columns